WIZlib Library API  ver 1.0
WIZlib Library API User Menual
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
httpd.c
Go to the documentation of this file.
1 
12 #include "common/common.h"
13 
14 
15 #define HTTPD_INCLUDE
16 #include "protocol/HTTP/httpd.h"
17 #undef HTTPD_INCLUDE
18 
19 extern char homepage_default[];
20 char tx_buf[MAX_URI_SIZE];
21 char rx_buf[MAX_URI_SIZE];
22 uint8 BUFPUB[1024];
23 
28  char * str,
29  char oldchar,
30  char newchar
31  )
32 {
33  int x;
34  for (x = 0; str[x]; x++)
35  if (str[x] == oldchar) str[x] = newchar;
36 }
43 char C2D(
44  uint8 c
45  )
46 {
47  if (c >= '0' && c <= '9')
48  return c - '0';
49  if (c >= 'a' && c <= 'f')
50  return 10 + c -'a';
51  if (c >= 'A' && c <= 'F')
52  return 10 + c -'A';
53 
54  return (char)c;
55 }
56 
61  char * url
62  )
63 {
64  int x, y;
65 
66  for (x = 0, y = 0; url[y]; ++x, ++y) {
67  if ((url[x] = url[y]) == '%') {
68  url[x] = C2D(url[y+1])*0x10+C2D(url[y+2]);
69  y+=2;
70  }
71  }
72  url[x] = '\0';
73 }
74 
75 
80  uint8 * buf,
81  int8 type,
82  uint32 len
83  )
84 {
85  char * head;
86  char tmp[10];
87 
88 
89  /* file type*/
90  if (type == PTYPE_HTML) head = RES_HTMLHEAD_OK;
91  else if (type == PTYPE_CGI) head = RES_HTMLHEAD_OK;
92  else if (type == PTYPE_PL) head = RES_HTMLHEAD_OK;
93  else if (type == PTYPE_GIF) head = RES_GIFHEAD_OK;
94  else if (type == PTYPE_TEXT) head = RES_TEXTHEAD_OK;
95  else if (type == PTYPE_JPEG) head = RES_JPEGHEAD_OK;
96  else if (type == PTYPE_FLASH) head = RES_FLASHHEAD_OK;
97  else if (type == PTYPE_MPEG) head = RES_MPEGHEAD_OK;
98  else if (type == PTYPE_PDF) head = RES_PDFHEAD_OK;
99  #ifdef HTTPD_DEBUG
100  else PRINTLN("\r\n\r\n-MAKE HEAD UNKNOWN-\r\n");
101  #endif
102 
103  sprintf(tmp,"%ld", len);
104  strcpy((char*)buf, head);
105  strcat((char*)buf, tmp);
106  strcat((char*)buf, "\r\n\r\n");
107 }
108 
109 
114  uint8 * type,
115  char * buf
116  )
117 {
118  /* Decide type according to extention*/
119  if (strstr(buf, ".pl")) *type = PTYPE_PL;
120  else if (strstr(buf, ".cgi") || strstr(buf,".CGI")) *type = PTYPE_CGI;
121  else if (strstr(buf, ".html") || strstr(buf,".htm")) *type = PTYPE_HTML;
122  else if (strstr(buf, ".gif")) *type = PTYPE_GIF;
123  else if (strstr(buf, ".text") || strstr(buf,".txt")) *type = PTYPE_TEXT;
124  else if (strstr(buf, ".jpeg") || strstr(buf,".jpg")) *type = PTYPE_JPEG;
125  else if (strstr(buf, ".swf")) *type = PTYPE_FLASH;
126  else if (strstr(buf, ".mpeg") || strstr(buf,".mpg")) *type = PTYPE_MPEG;
127  else if (strstr(buf, ".pdf")) *type = PTYPE_PDF;
128  else if (strstr(buf, ".js") || strstr(buf,".JS")) *type = PTYPE_TEXT;
129  else if (strstr(buf, ".xml") || strstr(buf,".XML")) *type = PTYPE_HTML;
130  else *type = PTYPE_ERR;
131 }
132 
133 
138  st_http_request * request,
139  char * buf
140  )
141 {
142  char * nexttok;
143  nexttok = strtok(buf," ");
144  if(!nexttok)
145  {
146  request->METHOD = METHOD_ERR;
147  return;
148  }
149  if(!strcmp(nexttok, "GET") || !strcmp(nexttok,"get"))
150  {
151  request->METHOD = METHOD_GET;
152  nexttok = strtok(NULL," ");
153  #ifdef HTTPD_DEBUG
154  PRINTLN("METHOD_GET");
155  #endif
156  }
157  else if (!strcmp(nexttok, "HEAD") || !strcmp(nexttok,"head"))
158  {
159  request->METHOD = METHOD_HEAD;
160  nexttok = strtok(NULL," ");
161  #ifdef HTTPD_DEBUG
162  PRINTLN("METHOD_HEAD");
163  #endif
164  }
165  else if (!strcmp(nexttok, "POST") || !strcmp(nexttok,"post"))
166  {
167  nexttok = strtok(NULL," ");
168  request->METHOD = METHOD_POST;
169  #ifdef HTTPD_DEBUG
170  PRINTLN("METHOD_POST");
171  #endif
172  }
173  else
174  {
175  request->METHOD = METHOD_ERR;
176  #ifdef HTTPD_DEBUG
177  PRINTLN("METHOD_ERR");
178  #endif
179  }
180 
181  if(!nexttok)
182  {
183  request->METHOD = METHOD_ERR;
184  #ifdef HTTPD_DEBUG
185  PRINTLN("METHOD_ERR");
186  #endif
187  return;
188  }
189 
190  strcpy(request->URI,nexttok);
191  nexttok = strtok(NULL,"\0");
192  strcpy(request->param,nexttok);
193 
194  #ifdef HTTPD_DEBUG
195  {
196  uint16 i;
197  printf("http_request->URI");
198  for(i=0; i < strlen(request->URI);i++)
199  printf("%c",request->URI[i]);
200  printf("\r\n");
201  }
202  #endif
203 
204 }
205 
206 
210 unsigned char* get_http_param_value(char* uri, char* param_name)
211 {
212 
213  char * name=0;
214  uint8 *ret=BUFPUB;
215  if(!uri || !param_name) return 0;
216  if((name = strstr(uri,param_name)))
217  {
218  name += strlen(param_name) + 1;
219  char* pos2=strstr(name,"&");
220  if(!pos2)
221  {
222  pos2=name+strlen(name);
223  }
224  uint16 len=0;
225  len = pos2-name;
226 
227  if(len)
228  {
229  ret[len]=0;
230  strncpy((char*)ret,name,len);
231  unescape_http_url((char*)ret);
232  replacetochar((char*)ret,'+',' ');
233  //ret[len]=0;
234  //ret[strlen((int8*)ret)]=0;
235  }
236  else
237  ret[0]=0;
238  }
239  else
240  return 0;
241  return ret;
242 }
243 
244 unsigned char* get_http_uri_name(char* uri)
245 {
246  char* uri_name;
247  char* tempURI=(char*)BUFPUB;
248 
249  if(!uri) return 0;
250 
251  memset (tempURI, 0, MAX_URI_SIZE);
252  strcpy(tempURI,uri);
253 
254  uri_name = strtok(tempURI," ?");
255 
256  if(strcmp(uri_name,"/")) uri_name++;
257 
258 #ifdef HTTPD_DEBUG
259  PRINTLN1("uri_name=%s",uri_name);
260 #endif
261 
262  return (uint8*)uri_name;
263 }