X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=bertos%2Fnet%2Fhttp.c;h=29146d405161d2f379298451a5fb6aeb9995ff52;hb=HEAD;hp=1121362dfb17028fe103805a97142ff2ac0aec0c;hpb=ed5ab8253db569e0ed85a35cc9beff02a2d204bc;p=bertos.git diff --git a/bertos/net/http.c b/bertos/net/http.c index 1121362d..29146d40 100644 --- a/bertos/net/http.c +++ b/bertos/net/http.c @@ -38,6 +38,8 @@ * the cases where SD is not present or page not found, using embedded pages. * Quering from browser the /status page, the server return a json dictionary where are store * some board status info, like board temperature, up-time, etc. + * + * notest: avr */ #include "http.h" @@ -53,91 +55,242 @@ #include #include +#include #include - -static const char http_html_hdr_200[] = "HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n"; -static const char http_html_hdr_404[] = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\n\r\n"; -static const char http_html_hdr_500[] = "HTTP/1.1 500 Internal Server Error\r\nContent-type: text/html\r\n\r\n"; +static struct { const char *key; const char *content; } http_content_type[] = +{ + {"", "Content-type: application/json\r\n\r\n"}, + {"htm", "Content-type: text/html\r\n\r\n"}, + {"css", "Content-type: text/css\r\n\r\n"}, + {"js", "Content-type: text/javascript\r\n\r\n"}, + {"png", "Content-type: image/png\r\n\r\n"}, + {"jpg", "Content-type: image/jpeg\r\n\r\n"}, + {"gif", "Content-type: image/gif\r\n\r\n"}, + {"txt", "Content-type: text/plain\r\n\r\n"}, +}; + +static const char http_html_hdr_200[] = "HTTP/1.0 200 OK\r\n"; +static const char http_html_hdr_404[] = "HTTP/1.0 404 Not Found\r\n"; +static const char http_html_hdr_500[] = "HTTP/1.0 500 Internal Server Error\r\n"; static HttpCGI *cgi_table; static http_handler_t http_callback; +static char decoded_str[80]; /** - * Send on \param client socket - * the 200 Ok http header + * Get key value from tokenized buffer */ -void http_sendOk(struct netconn *client) +int http_getValue(char *tolenized_buf, size_t tolenized_buf_len, const char *key, char *value, size_t len) { - netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY); -} + if (!tolenized_buf || !key || !value) + return -1; + char *p = tolenized_buf; + size_t value_len = 0; -/** - * Send on \param client socket - * the 404 File not found http header - */ -void http_sendFileNotFound(struct netconn *client) -{ - netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY); + memset(value, 0, len); + + for (size_t i = 0; i < tolenized_buf_len; i++) + { + size_t token_len = strlen(p); + http_decodeUrl(p, token_len, decoded_str, sizeof(decoded_str)); + + if (!strcmp(key, decoded_str)) + { + /* skip key */ + p += token_len + 1; + + value_len = http_decodeUrl(p, strlen(p), decoded_str, sizeof(decoded_str)); + + if (value_len >= len) + return -1; + + strcpy(value, decoded_str); + return value_len; + } + /* jump to next pair */ + p += token_len + 1; + } + + return -1; } /** - * Send on \param client socket - * the 500 internal server error http header + * tokenize a buffer */ -void http_sendInternalErr(struct netconn *client) +int http_tokenizeGetRequest(char *raw_buf, size_t raw_len) { - netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY); -} + size_t token = 0; -static void get_fileName(const char *revc_buf, size_t recv_len, char *name, size_t len) -{ - int i = 0; - char *p = strstr(revc_buf, "GET"); - if (p) + for(size_t i = 0; (i < raw_len) && raw_buf; i++) { - //Find the end of the page request. - char *stop = strstr(revc_buf, "HTTP"); - if (!stop) + if (raw_buf[i] == '&') { - LOG_ERR("Bad GET request\n"); - name[0] = '\0'; - return; + token++; + raw_buf[i] = '\0'; } - //skip the "/" in get string request - p += sizeof("GET") + 1; + if (raw_buf[i] == '=') + raw_buf[i] = '\0'; + } + + return token + 1; +} - while (p != stop) +static char http_hexToAscii(char first, char second) +{ + char hex[5], *stop; + hex[0] = '0'; + hex[1] = 'x'; + hex[2] = first; + hex[3] = second; + hex[4] = 0; + return strtol(hex, &stop, 16); +} + +size_t http_decodeUrl(const char *raw_buf, size_t raw_len, char *decodec_buf, size_t len) +{ + ASSERT(decodec_buf); + + char value; + size_t i; + memset(decodec_buf, 0, len); + + for (i = 0; i < raw_len; i++) + { + if (len <= 1) + return i; + + if (raw_buf[i] == '%') { - if ((size_t)i == len || (size_t)i >= recv_len) + if (i + 2 < raw_len) { - name[i] = '\0'; - break; + /* convert hex value after % */ + value = http_hexToAscii(raw_buf[i + 1], raw_buf[i + 2]); + if (value) + { + *decodec_buf++ = value; + len--; + /* decoded two digit of hex value, go to next value*/ + i += 2; + continue; + } } + } - name[i++] = *(p++); + /* Manage special case of '+', that it should be convert in space */ + *decodec_buf++ = (raw_buf[i] == '+' ? ' ' : raw_buf[i]); + len--; + } + + return i; +} + +void http_getPageName(const char *recv_buf, size_t recv_len, char *page_name, size_t len) +{ + int i = 0; + bool str_ok = false; + const char *p = recv_buf; + if (p && (recv_len > sizeof("GET /"))) + { + if (*p++ == 'G' && *p++ == 'E' && *p++ == 'T') + { + str_ok = true; + /* skip the space and "/" */ + p += 2; } } - //Trail white space in the string. - while ( --i >= 0 ) - if (name[i] != ' ' && name[i] != '\t' && name[i] != '\n') - break; + if (str_ok) + { + while ((size_t)i < recv_len) + { + char ch = *(p++); + if (ch == ' ' || ch == '\t' || ch == '\n') + break; + if((size_t)i == len - 1) + break; + page_name[i++] = ch; + } + } - name[i + 1] = '\0'; + page_name[i] = '\0'; } INLINE const char *get_ext(const char *name) { const char *ext = strstr(name, "."); - if(ext && (ext + 1)) + if(ext != NULL && ext[1] != '\0') return (ext + 1); return NULL; } +int http_searchContentType(const char *name) +{ + if (!name) + return 0; + + const char *ext = get_ext(name); + LOG_INFO("Ext: %s\n", !ext ? "none" : ext); + + if (!ext) + return 0; + + if (!strcmp(ext, "ico")) + return HTTP_CONTENT_JPEG; + + for (int i = 0; i < HTTP_CONTENT_CNT; i++) + { + if (!strcmp(ext, http_content_type[i].key)) + return i; + } + + return 0; +} + + +/** + * Send on \param client socket the 200 Ok http header with + * select \param content_type + */ +void http_sendOk(struct netconn *client, int content_type) +{ + ASSERT(content_type < HTTP_CONTENT_CNT); + + netconn_write(client, http_html_hdr_200, sizeof(http_html_hdr_200) - 1, NETCONN_NOCOPY); + netconn_write(client, http_content_type[content_type].content, + strlen(http_content_type[content_type].content), NETCONN_NOCOPY); +} + + +/** + * Send on \param client socket the 404 File not found http header with + * select \param content_type + */ +void http_sendFileNotFound(struct netconn *client, int content_type) +{ + ASSERT(content_type < HTTP_CONTENT_CNT); + + netconn_write(client, http_html_hdr_404, sizeof(http_html_hdr_404) - 1, NETCONN_NOCOPY); + netconn_write(client, http_content_type[content_type].content, + strlen(http_content_type[content_type].content), NETCONN_NOCOPY); +} + +/** + * Send on \param client socket the 500 internal server error http header with + * select \param content_type + */ +void http_sendInternalErr(struct netconn *client, int content_type) +{ + ASSERT(content_type < HTTP_CONTENT_CNT); + + netconn_write(client, http_html_hdr_500, sizeof(http_html_hdr_500) - 1, NETCONN_NOCOPY); + netconn_write(client, http_content_type[content_type].content, + strlen(http_content_type[content_type].content), NETCONN_NOCOPY); +} + static http_handler_t cgi_search(const char *name, HttpCGI *table) { if (!table) @@ -145,20 +298,35 @@ static http_handler_t cgi_search(const char *name, HttpCGI *table) int i = 0; const char *ext = get_ext(name); - LOG_INFO("EXT %s\n", ext); + while(table[i].name) { if (ext && table[i].type == CGI_MATCH_EXT) { - LOG_INFO("Match all ext %s\n", ext); + if (!strcmp(table[i].name, ext)) + { + LOG_INFO("Match ext: %s\n", ext); + break; + } + } + else if (table[i].type == CGI_MATCH_NAME) + { + + if (strstr(name, table[i].name) != NULL) + { + LOG_INFO("Match string: %s\n", name); break; + } } - else /* (table[i].type == CGI_MATCH_NAME) */ + else /* (table[i].type == CGI_MATCH_WORD) */ { - LOG_INFO("Match all name %s\n", name); + if (!strcmp(table[i].name, name)) + { + LOG_INFO("Word match: %s\n", name); break; + } } i++; @@ -193,9 +361,8 @@ void http_poll(struct netconn *server) if (rx_buf) { memset(req_string, 0, sizeof(req_string)); - get_fileName(rx_buf, len, req_string, sizeof(req_string)); + http_getPageName(rx_buf, len, req_string, sizeof(req_string)); - LOG_INFO("Search %s\n", req_string); if (req_string[0] == '\0') strcpy(req_string, HTTP_DEFAULT_PAGE); @@ -205,7 +372,7 @@ void http_poll(struct netconn *server) if (cgi(client, req_string, rx_buf, len) < 0) { LOG_ERR("Internal server error\n"); - http_sendInternalErr(client); + http_sendInternalErr(client, HTTP_CONTENT_HTML); netconn_write(client, http_server_error, http_server_error_len - 1, NETCONN_NOCOPY); } }