From 28fe0060d945a12ffb426fa944155ddaecf09cb6 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Wed, 17 Dec 2014 10:40:16 +0100 Subject: [PATCH] httpd: - separated content-types from other header fields to make it easier to add more content types (no manual indexing required); - added content-type for pdf, fixed content-type for javascript (no "x-"); --- apps/httpserver_raw/httpd.c | 5 +- apps/httpserver_raw/httpd_structs.h | 84 ++++++++++----------- apps/httpserver_raw/makefsdata/makefsdata.c | 7 +- 3 files changed, 45 insertions(+), 51 deletions(-) diff --git a/apps/httpserver_raw/httpd.c b/apps/httpserver_raw/httpd.c index 7e417cd..772b179 100644 --- a/apps/httpserver_raw/httpd.c +++ b/apps/httpserver_raw/httpd.c @@ -958,8 +958,7 @@ get_http_headers(struct http_state *pState, char *pszURI) for(iLoop = 0; (iLoop < NUM_HTTP_HEADERS) && pszExt; iLoop++) { /* Have we found a matching extension? */ if(!strcmp(g_psHTTPHeaders[iLoop].extension, pszExt)) { - pState->hdrs[2] = - g_psHTTPHeaderStrings[g_psHTTPHeaders[iLoop].headerIndex]; + pState->hdrs[2] = g_psHTTPHeaders[iLoop].content_type; break; } } @@ -981,7 +980,7 @@ get_http_headers(struct http_state *pState, char *pszURI) /* Did we find a matching extension? */ if(iLoop == NUM_HTTP_HEADERS) { /* No - use the default, plain text file type. */ - pState->hdrs[2] = g_psHTTPHeaderStrings[HTTP_HDR_DEFAULT_TYPE]; + pState->hdrs[2] = HTTP_HDR_DEFAULT_TYPE; } /* Set up to send the first header string. */ diff --git a/apps/httpserver_raw/httpd_structs.h b/apps/httpserver_raw/httpd_structs.h index 197d0ce..a49ce6d 100644 --- a/apps/httpserver_raw/httpd_structs.h +++ b/apps/httpserver_raw/httpd_structs.h @@ -24,26 +24,13 @@ typedef struct { const char *extension; - int headerIndex; + const char *content_type; } tHTTPHeader; -/** A list of strings used in HTTP headers */ +/** A list of strings used in HTTP headers (see RFC 1945 HTTP/1.0 and + * RFC 2616 HTTP/1.1 for header field definitions) */ static const char * const g_psHTTPHeaderStrings[] = { - "Content-type: text/html\r\n\r\n", - "Content-type: text/html\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\nPragma: no-cache\r\n\r\n", - "Content-type: image/gif\r\n\r\n", - "Content-type: image/png\r\n\r\n", - "Content-type: image/jpeg\r\n\r\n", - "Content-type: image/bmp\r\n\r\n", - "Content-type: image/x-icon\r\n\r\n", - "Content-type: application/octet-stream\r\n\r\n", - "Content-type: application/x-javascript\r\n\r\n", - "Content-type: application/x-javascript\r\n\r\n", - "Content-type: text/css\r\n\r\n", - "Content-type: application/x-shockwave-flash\r\n\r\n", - "Content-type: text/xml\r\n\r\n", - "Content-type: text/plain\r\n\r\n", "HTTP/1.0 200 OK\r\n", "HTTP/1.0 404 File not found\r\n", "HTTP/1.0 400 Bad Request\r\n", @@ -60,35 +47,41 @@ static const char * const g_psHTTPHeaderStrings[] = }; /* Indexes into the g_psHTTPHeaderStrings array */ -#define HTTP_HDR_HTML 0 /* text/html */ -#define HTTP_HDR_SSI 1 /* text/html Expires... */ -#define HTTP_HDR_GIF 2 /* image/gif */ -#define HTTP_HDR_PNG 3 /* image/png */ -#define HTTP_HDR_JPG 4 /* image/jpeg */ -#define HTTP_HDR_BMP 5 /* image/bmp */ -#define HTTP_HDR_ICO 6 /* image/x-icon */ -#define HTTP_HDR_APP 7 /* application/octet-stream */ -#define HTTP_HDR_JS 8 /* application/x-javascript */ -#define HTTP_HDR_RA 9 /* application/x-javascript */ -#define HTTP_HDR_CSS 10 /* text/css */ -#define HTTP_HDR_SWF 11 /* application/x-shockwave-flash */ -#define HTTP_HDR_XML 12 /* text/xml */ -#define HTTP_HDR_DEFAULT_TYPE 13 /* text/plain */ -#define HTTP_HDR_OK 14 /* 200 OK */ -#define HTTP_HDR_NOT_FOUND 15 /* 404 File not found */ -#define HTTP_HDR_BAD_REQUEST 16 /* 400 Bad request */ -#define HTTP_HDR_NOT_IMPL 17 /* 501 Not Implemented */ -#define HTTP_HDR_OK_11 18 /* 200 OK */ -#define HTTP_HDR_NOT_FOUND_11 19 /* 404 File not found */ -#define HTTP_HDR_BAD_REQUEST_11 20 /* 400 Bad request */ -#define HTTP_HDR_NOT_IMPL_11 21 /* 501 Not Implemented */ -#define HTTP_HDR_CONTENT_LENGTH 22 /* Content-Length: (HTTP 1.1)*/ -#define HTTP_HDR_CONN_CLOSE 23 /* Connection: Close (HTTP 1.1) */ -#define HTTP_HDR_CONN_KEEPALIVE 24 /* Connection: keep-alive (HTTP 1.1) */ -#define HTTP_HDR_SERVER 25 /* Server: HTTPD_SERVER_AGENT */ -#define DEFAULT_404_HTML 26 /* default 404 body */ +#define HTTP_HDR_OK 0 /* 200 OK */ +#define HTTP_HDR_NOT_FOUND 1 /* 404 File not found */ +#define HTTP_HDR_BAD_REQUEST 2 /* 400 Bad request */ +#define HTTP_HDR_NOT_IMPL 3 /* 501 Not Implemented */ +#define HTTP_HDR_OK_11 4 /* 200 OK */ +#define HTTP_HDR_NOT_FOUND_11 5 /* 404 File not found */ +#define HTTP_HDR_BAD_REQUEST_11 6 /* 400 Bad request */ +#define HTTP_HDR_NOT_IMPL_11 7 /* 501 Not Implemented */ +#define HTTP_HDR_CONTENT_LENGTH 8 /* Content-Length: (HTTP 1.1)*/ +#define HTTP_HDR_CONN_CLOSE 9 /* Connection: Close (HTTP 1.1) */ +#define HTTP_HDR_CONN_KEEPALIVE 10 /* Connection: keep-alive (HTTP 1.1) */ +#define HTTP_HDR_SERVER 11 /* Server: HTTPD_SERVER_AGENT */ +#define DEFAULT_404_HTML 12 /* default 404 body */ -/** A list of extension-to-HTTP header strings */ + +#define HTTP_HDR_HTML "Content-type: text/html\r\n\r\n" +#define HTTP_HDR_SSI "Content-type: text/html\r\nExpires: Fri, 10 Apr 2008 14:00:00 GMT\r\nPragma: no-cache\r\n\r\n" +#define HTTP_HDR_GIF "Content-type: image/gif\r\n\r\n" +#define HTTP_HDR_PNG "Content-type: image/png\r\n\r\n" +#define HTTP_HDR_JPG "Content-type: image/jpeg\r\n\r\n" +#define HTTP_HDR_BMP "Content-type: image/bmp\r\n\r\n" +#define HTTP_HDR_ICO "Content-type: image/x-icon\r\n\r\n" +#define HTTP_HDR_APP "Content-type: application/octet-stream\r\n\r\n" +#define HTTP_HDR_JS "Content-type: application/javascript\r\n\r\n" +#define HTTP_HDR_RA "Content-type: application/javascript\r\n\r\n" +#define HTTP_HDR_CSS "Content-type: text/css\r\n\r\n" +#define HTTP_HDR_SWF "Content-type: application/x-shockwave-flash\r\n\r\n" +#define HTTP_HDR_XML "Content-type: text/xml\r\n\r\n" +#define HTTP_HDR_PDF "Content-type: application/pdf\r\n\r\n" + +#define HTTP_HDR_DEFAULT_TYPE "Content-type: text/plain\r\n\r\n" + +/** A list of extension-to-HTTP header strings (see outdated RFC 1700 MEDIA TYPES + * and http://www.iana.org/assignments/media-types for registered content types + * and subtypes) */ static const tHTTPHeader g_psHTTPHeaders[] = { { "html", HTTP_HDR_HTML}, @@ -108,7 +101,8 @@ static const tHTTPHeader g_psHTTPHeaders[] = { "css", HTTP_HDR_CSS}, { "swf", HTTP_HDR_SWF}, { "xml", HTTP_HDR_XML}, - { "xsl", HTTP_HDR_XML} + { "xsl", HTTP_HDR_XML}, + { "pdf", HTTP_HDR_PDF} }; #define NUM_HTTP_HEADERS (sizeof(g_psHTTPHeaders) / sizeof(tHTTPHeader)) diff --git a/apps/httpserver_raw/makefsdata/makefsdata.c b/apps/httpserver_raw/makefsdata/makefsdata.c index 8e5c551..42f7d4f 100644 --- a/apps/httpserver_raw/makefsdata/makefsdata.c +++ b/apps/httpserver_raw/makefsdata/makefsdata.c @@ -584,7 +584,7 @@ int file_write_http_header(FILE *data_file, const char *filename, int file_size, { int i = 0; int response_type = HTTP_HDR_OK; - int file_type = HTTP_HDR_DEFAULT_TYPE; + const char* file_type; const char *cur_string; size_t cur_len; int written = 0; @@ -656,10 +656,11 @@ int file_write_http_header(FILE *data_file, const char *filename, int file_size, } if((file_ext == NULL) || (*file_ext == 0)) { printf("failed to get extension for file \"%s\", using default.\n", filename); + file_type = HTTP_HDR_DEFAULT_TYPE; } else { for(j = 0; j < NUM_HTTP_HEADERS; j++) { if(!strcmp(file_ext, g_psHTTPHeaders[j].extension)) { - file_type = g_psHTTPHeaders[j].headerIndex; + file_type = g_psHTTPHeaders[j].content_type; break; } } @@ -713,7 +714,7 @@ int file_write_http_header(FILE *data_file, const char *filename, int file_size, } } - cur_string = g_psHTTPHeaderStrings[file_type]; + cur_string = file_type; cur_len = strlen(cur_string); fprintf(data_file, NEWLINE "/* \"%s\" (%d bytes) */" NEWLINE, cur_string, cur_len); written += file_put_ascii(data_file, cur_string, cur_len, &i);