see http://mbed.org/users/okini3939/notebook/wattmeter-shield-on-mbed/
Fork of GSwifi_xively by
GSwifiInterface/GSwifi/GSwifi_httpd_util.cpp@4:9a2415f2ab07, 2013-11-27 (annotated)
- Committer:
- okini3939
- Date:
- Wed Nov 27 08:18:45 2013 +0000
- Revision:
- 4:9a2415f2ab07
update GSwifiInterface library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 4:9a2415f2ab07 | 1 | /* Copyright (C) 2013 gsfan, MIT License |
okini3939 | 4:9a2415f2ab07 | 2 | * |
okini3939 | 4:9a2415f2ab07 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
okini3939 | 4:9a2415f2ab07 | 4 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
okini3939 | 4:9a2415f2ab07 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
okini3939 | 4:9a2415f2ab07 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
okini3939 | 4:9a2415f2ab07 | 7 | * furnished to do so, subject to the following conditions: |
okini3939 | 4:9a2415f2ab07 | 8 | * |
okini3939 | 4:9a2415f2ab07 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
okini3939 | 4:9a2415f2ab07 | 10 | * substantial portions of the Software. |
okini3939 | 4:9a2415f2ab07 | 11 | * |
okini3939 | 4:9a2415f2ab07 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
okini3939 | 4:9a2415f2ab07 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
okini3939 | 4:9a2415f2ab07 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
okini3939 | 4:9a2415f2ab07 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
okini3939 | 4:9a2415f2ab07 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
okini3939 | 4:9a2415f2ab07 | 17 | */ |
okini3939 | 4:9a2415f2ab07 | 18 | |
okini3939 | 4:9a2415f2ab07 | 19 | #include "GSwifi.h" |
okini3939 | 4:9a2415f2ab07 | 20 | |
okini3939 | 4:9a2415f2ab07 | 21 | #ifdef CFG_ENABLE_HTTPD |
okini3939 | 4:9a2415f2ab07 | 22 | |
okini3939 | 4:9a2415f2ab07 | 23 | int GSwifi::httpdFile (int cid, char *dir) { |
okini3939 | 4:9a2415f2ab07 | 24 | FILE *fp; |
okini3939 | 4:9a2415f2ab07 | 25 | int i, len; |
okini3939 | 4:9a2415f2ab07 | 26 | char buf[CFG_DATA_SIZE]; |
okini3939 | 4:9a2415f2ab07 | 27 | char file[CFG_CMD_SIZE]; |
okini3939 | 4:9a2415f2ab07 | 28 | |
okini3939 | 4:9a2415f2ab07 | 29 | INFO("httpdFile %d %s", cid, dir); |
okini3939 | 4:9a2415f2ab07 | 30 | |
okini3939 | 4:9a2415f2ab07 | 31 | strcpy(file, dir); |
okini3939 | 4:9a2415f2ab07 | 32 | strcat(file, _httpd[cid].filename); |
okini3939 | 4:9a2415f2ab07 | 33 | if (file[strlen(file) - 1] == '/') { |
okini3939 | 4:9a2415f2ab07 | 34 | strcat(file, "index.html"); |
okini3939 | 4:9a2415f2ab07 | 35 | } |
okini3939 | 4:9a2415f2ab07 | 36 | DBG("file: %s\r\n", file); |
okini3939 | 4:9a2415f2ab07 | 37 | |
okini3939 | 4:9a2415f2ab07 | 38 | fp = fopen(file, "r"); |
okini3939 | 4:9a2415f2ab07 | 39 | if (fp) { |
okini3939 | 4:9a2415f2ab07 | 40 | strcpy(buf, "HTTP/1.1 200 OK\r\n"); |
okini3939 | 4:9a2415f2ab07 | 41 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 42 | { |
okini3939 | 4:9a2415f2ab07 | 43 | // file size |
okini3939 | 4:9a2415f2ab07 | 44 | i = ftell(fp); |
okini3939 | 4:9a2415f2ab07 | 45 | fseek(fp, 0, SEEK_END); |
okini3939 | 4:9a2415f2ab07 | 46 | len = ftell(fp); |
okini3939 | 4:9a2415f2ab07 | 47 | fseek(fp, i, SEEK_SET); |
okini3939 | 4:9a2415f2ab07 | 48 | } |
okini3939 | 4:9a2415f2ab07 | 49 | |
okini3939 | 4:9a2415f2ab07 | 50 | strcpy(buf, "Server: GSwifi httpd\r\n"); |
okini3939 | 4:9a2415f2ab07 | 51 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 52 | if (_httpd[cid].keepalive) { |
okini3939 | 4:9a2415f2ab07 | 53 | strcpy(buf, "Connection: Keep-Alive\r\n"); |
okini3939 | 4:9a2415f2ab07 | 54 | } else { |
okini3939 | 4:9a2415f2ab07 | 55 | strcpy(buf, "Connection: close\r\n"); |
okini3939 | 4:9a2415f2ab07 | 56 | } |
okini3939 | 4:9a2415f2ab07 | 57 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 58 | sprintf(buf, "Content-Type: %s\r\n", mimetype(file)); |
okini3939 | 4:9a2415f2ab07 | 59 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 60 | sprintf(buf, "Content-Length: %d\r\n\r\n", len); |
okini3939 | 4:9a2415f2ab07 | 61 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 62 | |
okini3939 | 4:9a2415f2ab07 | 63 | for (;;) { |
okini3939 | 4:9a2415f2ab07 | 64 | i = fread(buf, sizeof(char), sizeof(buf), fp); |
okini3939 | 4:9a2415f2ab07 | 65 | if (i <= 0) break; |
okini3939 | 4:9a2415f2ab07 | 66 | send(cid, buf, i); |
okini3939 | 4:9a2415f2ab07 | 67 | #if defined(TARGET_LPC1768) || defined(TARGET_LPC2368) |
okini3939 | 4:9a2415f2ab07 | 68 | if (feof(fp)) break; |
okini3939 | 4:9a2415f2ab07 | 69 | #endif |
okini3939 | 4:9a2415f2ab07 | 70 | } |
okini3939 | 4:9a2415f2ab07 | 71 | fclose(fp); |
okini3939 | 4:9a2415f2ab07 | 72 | return 0; |
okini3939 | 4:9a2415f2ab07 | 73 | } |
okini3939 | 4:9a2415f2ab07 | 74 | |
okini3939 | 4:9a2415f2ab07 | 75 | httpdError(cid, 404); |
okini3939 | 4:9a2415f2ab07 | 76 | return -1; |
okini3939 | 4:9a2415f2ab07 | 77 | } |
okini3939 | 4:9a2415f2ab07 | 78 | |
okini3939 | 4:9a2415f2ab07 | 79 | void GSwifi::httpdError (int cid, int err) { |
okini3939 | 4:9a2415f2ab07 | 80 | char buf[CFG_CMD_SIZE], msg[30]; |
okini3939 | 4:9a2415f2ab07 | 81 | |
okini3939 | 4:9a2415f2ab07 | 82 | switch (err) { |
okini3939 | 4:9a2415f2ab07 | 83 | case 400: |
okini3939 | 4:9a2415f2ab07 | 84 | strcpy(msg, "Bad Request"); |
okini3939 | 4:9a2415f2ab07 | 85 | break; |
okini3939 | 4:9a2415f2ab07 | 86 | case 403: |
okini3939 | 4:9a2415f2ab07 | 87 | strcpy(msg, "Forbidden"); |
okini3939 | 4:9a2415f2ab07 | 88 | break; |
okini3939 | 4:9a2415f2ab07 | 89 | case 404: |
okini3939 | 4:9a2415f2ab07 | 90 | strcpy(msg, "Not Found"); |
okini3939 | 4:9a2415f2ab07 | 91 | break; |
okini3939 | 4:9a2415f2ab07 | 92 | case 500: |
okini3939 | 4:9a2415f2ab07 | 93 | default: |
okini3939 | 4:9a2415f2ab07 | 94 | strcpy(msg, "Internal Server Error"); |
okini3939 | 4:9a2415f2ab07 | 95 | break; |
okini3939 | 4:9a2415f2ab07 | 96 | } |
okini3939 | 4:9a2415f2ab07 | 97 | DBG("httpd error: %d %d %s\r\n", cid, err, msg); |
okini3939 | 4:9a2415f2ab07 | 98 | |
okini3939 | 4:9a2415f2ab07 | 99 | sprintf(buf, "HTTP/1.1 %d %s\r\n", err, msg); |
okini3939 | 4:9a2415f2ab07 | 100 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 101 | strcpy(buf, "Content-Type: text/html\r\n\r\n"); |
okini3939 | 4:9a2415f2ab07 | 102 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 103 | |
okini3939 | 4:9a2415f2ab07 | 104 | sprintf(buf, "<html><head><title>%d %s</title></head>\r\n", err, msg); |
okini3939 | 4:9a2415f2ab07 | 105 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 106 | sprintf(buf, "<body><h1>%s</h1></body></html>\r\n", msg); |
okini3939 | 4:9a2415f2ab07 | 107 | send(cid, buf, strlen(buf)); |
okini3939 | 4:9a2415f2ab07 | 108 | wait_ms(100); |
okini3939 | 4:9a2415f2ab07 | 109 | close(cid); |
okini3939 | 4:9a2415f2ab07 | 110 | // WARN("%d.%d.%d.%d ", _httpd[cid].host.getIp()[0], _httpd[cid].host.getIp()[1], _httpd[cid].host.getIp()[2], _httpd[cid].host.getIp()[3]); |
okini3939 | 4:9a2415f2ab07 | 111 | // WARN("%s %s %d %d -\r\n", _httpd[cid].type == GSPROT_HTTPGET ? "GET" : "POST", _httpd[cid].uri, _httpd[cid].length, err); |
okini3939 | 4:9a2415f2ab07 | 112 | } |
okini3939 | 4:9a2415f2ab07 | 113 | |
okini3939 | 4:9a2415f2ab07 | 114 | int GSwifi::httpdGetHandler (const char *uri) { |
okini3939 | 4:9a2415f2ab07 | 115 | int i; |
okini3939 | 4:9a2415f2ab07 | 116 | |
okini3939 | 4:9a2415f2ab07 | 117 | for (i = 0; i < _handler_count; i ++) { |
okini3939 | 4:9a2415f2ab07 | 118 | if (strncmp(uri, _httpd_handler[i].uri, strlen(_httpd_handler[i].uri)) == NULL) { |
okini3939 | 4:9a2415f2ab07 | 119 | // found |
okini3939 | 4:9a2415f2ab07 | 120 | return i; |
okini3939 | 4:9a2415f2ab07 | 121 | } |
okini3939 | 4:9a2415f2ab07 | 122 | } |
okini3939 | 4:9a2415f2ab07 | 123 | return -1; |
okini3939 | 4:9a2415f2ab07 | 124 | } |
okini3939 | 4:9a2415f2ab07 | 125 | |
okini3939 | 4:9a2415f2ab07 | 126 | int GSwifi::httpdAttach (const char *uri, const char *dir) { |
okini3939 | 4:9a2415f2ab07 | 127 | if (_handler_count < CFG_HTTPD_HANDLER_NUM) { |
okini3939 | 4:9a2415f2ab07 | 128 | _httpd_handler[_handler_count].uri = (char*)malloc(strlen(uri) + 1); |
okini3939 | 4:9a2415f2ab07 | 129 | strcpy(_httpd_handler[_handler_count].uri, uri); |
okini3939 | 4:9a2415f2ab07 | 130 | _httpd_handler[_handler_count].dir = (char*)malloc(strlen(dir) + 1); |
okini3939 | 4:9a2415f2ab07 | 131 | strcpy(_httpd_handler[_handler_count].dir, dir); |
okini3939 | 4:9a2415f2ab07 | 132 | _httpd_handler[_handler_count].func = NULL; |
okini3939 | 4:9a2415f2ab07 | 133 | DBG("httpdAttach %s %s\r\n", _httpd_handler[_handler_count].uri, _httpd_handler[_handler_count].dir); |
okini3939 | 4:9a2415f2ab07 | 134 | _handler_count ++; |
okini3939 | 4:9a2415f2ab07 | 135 | return 0; |
okini3939 | 4:9a2415f2ab07 | 136 | } else { |
okini3939 | 4:9a2415f2ab07 | 137 | return -1; |
okini3939 | 4:9a2415f2ab07 | 138 | } |
okini3939 | 4:9a2415f2ab07 | 139 | } |
okini3939 | 4:9a2415f2ab07 | 140 | |
okini3939 | 4:9a2415f2ab07 | 141 | int GSwifi::httpdAttach (const char *uri, void (*funcCgi)(int)) { |
okini3939 | 4:9a2415f2ab07 | 142 | if (_handler_count < CFG_HTTPD_HANDLER_NUM) { |
okini3939 | 4:9a2415f2ab07 | 143 | _httpd_handler[_handler_count].uri = (char*)malloc(strlen(uri) + 1); |
okini3939 | 4:9a2415f2ab07 | 144 | strcpy(_httpd_handler[_handler_count].uri, uri); |
okini3939 | 4:9a2415f2ab07 | 145 | _httpd_handler[_handler_count].dir = NULL; |
okini3939 | 4:9a2415f2ab07 | 146 | _httpd_handler[_handler_count].func = funcCgi; |
okini3939 | 4:9a2415f2ab07 | 147 | DBG("httpdAttach %s %08x\r\n", _httpd_handler[_handler_count].uri, _httpd_handler[_handler_count].func); |
okini3939 | 4:9a2415f2ab07 | 148 | _handler_count ++; |
okini3939 | 4:9a2415f2ab07 | 149 | return 0; |
okini3939 | 4:9a2415f2ab07 | 150 | } else { |
okini3939 | 4:9a2415f2ab07 | 151 | return -1; |
okini3939 | 4:9a2415f2ab07 | 152 | } |
okini3939 | 4:9a2415f2ab07 | 153 | } |
okini3939 | 4:9a2415f2ab07 | 154 | |
okini3939 | 4:9a2415f2ab07 | 155 | |
okini3939 | 4:9a2415f2ab07 | 156 | #define MIMETABLE_NUM 9 |
okini3939 | 4:9a2415f2ab07 | 157 | char *GSwifi::mimetype (char *file) { |
okini3939 | 4:9a2415f2ab07 | 158 | static const struct MIMETABLE { |
okini3939 | 4:9a2415f2ab07 | 159 | const char ext[5]; |
okini3939 | 4:9a2415f2ab07 | 160 | const char type[24]; |
okini3939 | 4:9a2415f2ab07 | 161 | } mimetable[MIMETABLE_NUM] = { |
okini3939 | 4:9a2415f2ab07 | 162 | {"txt", "text/plain"}, // default |
okini3939 | 4:9a2415f2ab07 | 163 | {"html", "text/html"}, |
okini3939 | 4:9a2415f2ab07 | 164 | {"htm", "text/html"}, |
okini3939 | 4:9a2415f2ab07 | 165 | {"css", "text/css"}, |
okini3939 | 4:9a2415f2ab07 | 166 | {"js", "application/javascript"}, |
okini3939 | 4:9a2415f2ab07 | 167 | {"jpg", "image/jpeg"}, |
okini3939 | 4:9a2415f2ab07 | 168 | {"png", "image/png"}, |
okini3939 | 4:9a2415f2ab07 | 169 | {"gif", "image/gif"}, |
okini3939 | 4:9a2415f2ab07 | 170 | {"ico", "image/x-icon"}, |
okini3939 | 4:9a2415f2ab07 | 171 | }; |
okini3939 | 4:9a2415f2ab07 | 172 | int i, j; |
okini3939 | 4:9a2415f2ab07 | 173 | |
okini3939 | 4:9a2415f2ab07 | 174 | for (i = 0; i < MIMETABLE_NUM; i ++) { |
okini3939 | 4:9a2415f2ab07 | 175 | j = strlen(mimetable[i].ext); |
okini3939 | 4:9a2415f2ab07 | 176 | if (file[strlen(file) - j - 1] == '.' && |
okini3939 | 4:9a2415f2ab07 | 177 | strnicmp(&file[strlen(file) - j], mimetable[i].ext, j) == NULL) { |
okini3939 | 4:9a2415f2ab07 | 178 | return (char*)mimetable[i].type; |
okini3939 | 4:9a2415f2ab07 | 179 | } |
okini3939 | 4:9a2415f2ab07 | 180 | } |
okini3939 | 4:9a2415f2ab07 | 181 | return (char*)mimetable[0].type; |
okini3939 | 4:9a2415f2ab07 | 182 | } |
okini3939 | 4:9a2415f2ab07 | 183 | |
okini3939 | 4:9a2415f2ab07 | 184 | int GSwifi::strnicmp (const char *p1, const char *p2, int n) { |
okini3939 | 4:9a2415f2ab07 | 185 | int i, r = -1; |
okini3939 | 4:9a2415f2ab07 | 186 | char c1, c2; |
okini3939 | 4:9a2415f2ab07 | 187 | |
okini3939 | 4:9a2415f2ab07 | 188 | for (i = 0; i < n; i ++) { |
okini3939 | 4:9a2415f2ab07 | 189 | c1 = (p1[i] >= 'a' && p1[i] <= 'z') ? p1[i] - ('a' - 'A'): p1[i]; |
okini3939 | 4:9a2415f2ab07 | 190 | c2 = (p2[i] >= 'a' && p2[i] <= 'z') ? p2[i] - ('a' - 'A'): p2[i]; |
okini3939 | 4:9a2415f2ab07 | 191 | r = c1 - c2; |
okini3939 | 4:9a2415f2ab07 | 192 | if (r) break; |
okini3939 | 4:9a2415f2ab07 | 193 | } |
okini3939 | 4:9a2415f2ab07 | 194 | return r; |
okini3939 | 4:9a2415f2ab07 | 195 | } |
okini3939 | 4:9a2415f2ab07 | 196 | #endif |