GainSpan Wi-Fi library see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Dependents: GSwifi_httpd GSwifi_websocket GSwifi_tcpclient GSwifi_tcpserver ... more
Fork of GSwifi by
GainSpan Wi-Fi library
The GS1011 is an ultra low power 802.11b wireless module from GainSpan.
see: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
ゲインスパン Wi-Fi モジュール ライブラリ
ゲインスパン社の低電力 Wi-Fiモジュール(無線LAN) GS1011 シリーズ用のライブラリです。
解説: http://mbed.org/users/gsfan/notebook/gainspan_wifi/
Diff: GSwifi_httpd.cpp
- Revision:
- 20:151b5a4fdd29
- Parent:
- 19:cad912f5a6ba
- Child:
- 22:9b077e2823ce
--- a/GSwifi_httpd.cpp Tue Nov 06 08:54:19 2012 +0000 +++ b/GSwifi_httpd.cpp Thu Nov 08 01:28:45 2012 +0000 @@ -2,18 +2,21 @@ #include "mbed.h" #include "GSwifi.h" +#ifdef GS_USE_HTTPD + #define HTTPD_REQUEST 0 #define HTTPD_HEAD 1 #define HTTPD_SPACE 2 #define HTTPD_BODY 3 #define HTTPD_ERROR 4 -#define MIMETABLE_NUM 6 +#define MIMETABLE_NUM 7 struct { - char ext[5]; - char type[24]; + char ext[6]; + char type[20]; } mimetable[MIMETABLE_NUM] = { {".txt", "text/plain"}, + {".html", "text/html"}, {".htm", "text/html"}, {".css", "text/css"}, {".jpg", "image/jpeg"}, @@ -78,7 +81,7 @@ if (j >= len) return; if (_httpd[cid].len < HTTPD_BUF_SIZE) { _httpd[cid].buf[_httpd[cid].len] = 0; - DBG("httpd: %d %s (%d)\r\n", _httpd[cid].mode, _httpd[cid].buf, _httpd[cid].len); + DBG("httpd %d: %d %s (%d)\r\n", cid, _httpd[cid].mode, _httpd[cid].buf, _httpd[cid].len); } switch (_httpd[cid].mode) { @@ -116,7 +119,7 @@ _httpd[cid].mode = HTTPD_BODY; if (_httpd[cid].length == 0) flg = 1; // no body } else - if (strncmp(_httpd[cid].buf, "Content-length: ", 16) == 0) { + if (strncmp(_httpd[cid].buf, "Content-Length: ", 16) == 0) { _httpd[cid].length = atoi(&_httpd[cid].buf[16]); } else if (strncmp(_httpd[cid].buf, "Connection: Keep-Alive", 22) == 0) { @@ -148,7 +151,7 @@ _httpd[cid].query = NULL; for (; j < strlen(_httpd[cid].uri); j ++) { if (_httpd[cid].uri[j] == '?') { - // query_string + // query string _httpd[cid].uri[j] = 0; _httpd[cid].query = &_httpd[cid].uri[j + 1]; break; @@ -164,6 +167,8 @@ // cgi _handler[i].onHttpCgi(cid, &_httpd[cid]); _httpd[cid].keepalive = 0; + LOG("%d.%d.%d.%d ", _httpd[cid].host.getIp()[0], _httpd[cid].host.getIp()[1], _httpd[cid].host.getIp()[2], _httpd[cid].host.getIp()[3]); + LOG("%s %s %d 200 -\r\n", _httpd[cid].type == GSPROT_HTTPGET ? "GET" : "POST", _httpd[cid].uri, _httpd[cid].length); flg = 1; } break; @@ -194,29 +199,30 @@ int GSwifi::httpd_request (int cid, GS_httpd *gshttpd, char *dir) { FILE *fp; - int i, j; - char buf[100]; + int i, len; + char buf[HTTPD_BUF_SIZE]; + char file[HTTPD_URI_SIZE]; - strcpy(buf, dir); - strcat(buf, gshttpd->file); - if (buf[strlen(buf) - 1] == '/') { - strcat(buf, "index.htm"); + strcpy(file, dir); + strcat(file, gshttpd->file); + if (file[strlen(file) - 1] == '/') { + strcat(file, "index.htm"); } - DBG("file: %s\r\n", buf); + DBG("file: %s\r\n", file); - fp = fopen(buf, "r"); + fp = fopen(file, "r"); if (fp) { send(cid, "HTTP/1.1 200 OK\r\n", 17); { // file size - j = ftell(fp); + i = ftell(fp); fseek(fp, 0, SEEK_END); - i = ftell(fp); - fseek(fp, j, SEEK_SET); + len = ftell(fp); + fseek(fp, i, SEEK_SET); } - sprintf(buf, "Content-Length: %d\r\n", i); + sprintf(buf, "Content-Length: %d\r\n", len); send(cid, buf, strlen(buf)); - sprintf(buf, "Content-Type: %s\r\n", mimetype(gshttpd->file)); + sprintf(buf, "Content-Type: %s\r\n", mimetype(file)); send(cid, buf, strlen(buf)); if (gshttpd->keepalive) { strcpy(buf, "Connection: Keep-Alive\r\n"); @@ -230,11 +236,15 @@ for (;;) { i = fread(buf, sizeof(char), sizeof(buf), fp); - if (i == 0) break; + if (i <= 0) break; send(cid, buf, i); +#if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) if (feof(fp)) break; +#endif } fclose(fp); + LOG("%d.%d.%d.%d ", _httpd[cid].host.getIp()[0], _httpd[cid].host.getIp()[1], _httpd[cid].host.getIp()[2], _httpd[cid].host.getIp()[3]); + LOG("%s %s %d 200 %d\r\n", _httpd[cid].type == GSPROT_HTTPGET ? "GET" : "POST", _httpd[cid].uri, _httpd[cid].length, len); return 0; } @@ -245,6 +255,7 @@ char *GSwifi::mimetype (char *file) { int i, j; + DBG("<%s>\r\n", file); for (i = 0; i < MIMETABLE_NUM; i ++) { j = strlen(mimetable[i].ext); if (strncmp(&file[strlen(file) - j], mimetable[i].ext, j) == NULL) { @@ -285,6 +296,8 @@ sprintf(buf, "<body><h1>%s</h1></body></html>\r\n", msg); send(cid, buf, strlen(buf)); close(cid); + LOG("%d.%d.%d.%d ", _httpd[cid].host.getIp()[0], _httpd[cid].host.getIp()[1], _httpd[cid].host.getIp()[2], _httpd[cid].host.getIp()[3]); + LOG("%s %s %d %d -\r\n", _httpd[cid].type == GSPROT_HTTPGET ? "GET" : "POST", _httpd[cid].uri, _httpd[cid].length, err); } int GSwifi::attach_httpd (const char *uri, const char *dir) { @@ -313,3 +326,5 @@ return -1; } } + +#endif