From dc65ef038279df55d5e49052eabac4eae2617ee1 Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Wed, 14 Dec 2016 09:14:39 -0500 Subject: Now compiles with Turbo C 2.0 using included makefile. Added better logging (to screen). Added supprot for opening and sending gzip'ed files if available on disk. --- httpd/http-str.c | 15 +++++++++++++ httpd/http-str.h | 5 +++++ httpd/httpd-fs.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++-------- httpd/httpd-fs.h | 4 ++++ httpd/httpd.c | 50 ++++++++++++++++++++++++++++++++++-------- httpd/httpd.h | 1 + 6 files changed, 124 insertions(+), 18 deletions(-) (limited to 'httpd') diff --git a/httpd/http-str.c b/httpd/http-str.c index e556f95..6b86ca6 100644 --- a/httpd/http-str.c +++ b/httpd/http-str.c @@ -83,9 +83,15 @@ const char http_shtml[7] = const char http_htm[5] = /* ".htm" */ {0x2e, 0x68, 0x74, 0x6d, }; +const char http_hgz[5] = +/* ".hgz" */ +{0x2e, 0x68, 0x67, 0x7a, }; const char http_css[5] = /* ".css" */ {0x2e, 0x63, 0x73, 0x73, }; +const char http_cgz[5] = +/* ".css" */ +{0x2e, 0x63, 0x67, 0x7a, }; const char http_png[5] = /* ".png" */ {0x2e, 0x70, 0x6e, 0x67, }; @@ -101,3 +107,12 @@ const char http_text[5] = const char http_txt[5] = /* ".txt" */ {0x2e, 0x74, 0x78, 0x74, }; +const char http_gzip[4] = +/* "gzip" */ +{0x67, 0x7a, 0x69, 0x70}; +const char http_accept_encode[17] = +/* "Accept-Encoding: " */ +{0x41, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x20}; +const char http_content_encode[24] = +/* "Content-Encoding: gzip\r\n" */ +{0x43, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x45, 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x3a, 0x20, 0x67, 0x7a, 0x69, 0x70, 0xd, 0xa}; diff --git a/httpd/http-str.h b/httpd/http-str.h index e0eb17d..712df71 100644 --- a/httpd/http-str.h +++ b/httpd/http-str.h @@ -27,9 +27,14 @@ extern const char http_html[6]; extern const char http_htm[5]; extern const char http_shtml[7]; extern const char http_htm[5]; +extern const char http_hgz[5]; extern const char http_css[5]; +extern const char http_cgz[5]; extern const char http_png[5]; extern const char http_gif[5]; extern const char http_jpg[5]; extern const char http_text[5]; extern const char http_txt[5]; +extern const char http_gzip[4]; +extern const char http_accept_encode[17]; +extern const char http_content_encode[24]; diff --git a/httpd/httpd-fs.c b/httpd/httpd-fs.c index 18d5729..a694fbc 100644 --- a/httpd/httpd-fs.c +++ b/httpd/httpd-fs.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009 Jeffrey Armstrong + * Copyright (c) 2009, 2016 Jeffrey Armstrong * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -36,17 +36,11 @@ #include #include #include +#include +#include #include "httpd-fs.h" -void change_to_backslash(char *name) -{ -int counter; - for(counter=0;counter +#include #define STATE_WAITING 0 #define STATE_OUTPUT 1 @@ -62,7 +63,7 @@ static unsigned short generate_part_of_file(void *state) fread(uip_appdata,sizeof(char),s->len,s->file.fp); - if(s->len == s->file.len) + if(s->len >= s->file.len) fclose(s->file.fp); return s->len; @@ -95,17 +96,22 @@ static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) char *ptr; PSOCK_BEGIN(&s->sout); - + PSOCK_SEND_STR(&s->sout, statushdr); + if(s->compressed) + PSOCK_SEND_STR(&s->sout, http_content_encode); + ptr = strrchr(s->filename, ISO_period); if(ptr == NULL) { PSOCK_SEND_STR(&s->sout, http_content_type_binary); } else if(strncmp(http_html, ptr, 5) == 0 || strncmp(http_shtml, ptr, 6) == 0 || - strncmp(http_htm, ptr, 4) == 0) { + strncmp(http_htm, ptr, 4) == 0 || + strncmp(http_hgz, ptr, 4) == 0) { PSOCK_SEND_STR(&s->sout, http_content_type_html); - } else if(strncmp(http_css, ptr, 4) == 0) { + } else if(strncmp(http_css, ptr, 4) == 0 || + strncmp(http_cgz, ptr, 4) == 0) { PSOCK_SEND_STR(&s->sout, http_content_type_css); } else if(strncmp(http_png, ptr, 4) == 0) { PSOCK_SEND_STR(&s->sout, http_content_type_png); @@ -116,6 +122,7 @@ static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) } else { PSOCK_SEND_STR(&s->sout, http_content_type_plain); } + PSOCK_END(&s->sout); } @@ -123,8 +130,19 @@ static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; + time_t ltime; PT_BEGIN(&s->outputpt); + + printf("%s requested - ", s->filename); + + if(s->compressed == 1 && compressed_available(s->filename) == 1) + compressed_name(s->filename); + else + s->compressed = 0; + + ltime = time(NULL); + printf("using %s - %s", s->filename, ctime(<ime)); if(httpd_fs_open(s->filename, &s->file) < 0) { @@ -141,15 +159,18 @@ static PT_THREAD(handle_output(struct httpd_state *s)) PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); - ptr = strchr(s->filename, ISO_period); + /* ptr = strchr(s->filename, ISO_period); */ PT_WAIT_THREAD(&s->outputpt, send_file(s)); } + PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); } static PT_THREAD(handle_input(struct httpd_state *s)) { + int i; + PSOCK_BEGIN(&s->sin); PSOCK_READTO(&s->sin, ISO_space); @@ -172,18 +193,29 @@ static PT_THREAD(handle_input(struct httpd_state *s)) } /* httpd_log_file(uip_conn->ripaddr, s->filename);*/ - - s->state = STATE_OUTPUT; - + s->compressed = 0; while(1) { PSOCK_READTO(&s->sin, ISO_nl); if(strncmp(s->inputbuf, http_referer, 8) == 0) { s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0; /* httpd_log(&s->inputbuf[9]);*/ + } else if(strncmp(s->inputbuf, http_accept_encode, 17) == 0) { + i = 17; + while(s->inputbuf[i] != ISO_nl) { + if(strncmp(&s->inputbuf[i], http_gzip, 4) == 0) { + s->compressed = 1; + break; + } + i++; + } + } else if(PSOCK_DATALEN(&s->sin) < 3) { + break; } + } + s->state = STATE_OUTPUT; PSOCK_END(&s->sin); } diff --git a/httpd/httpd.h b/httpd/httpd.h index 270d49d..ada06c1 100644 --- a/httpd/httpd.h +++ b/httpd/httpd.h @@ -49,6 +49,7 @@ struct httpd_state { int len; char *scriptptr; int scriptlen; + short compressed; unsigned short count; }; -- cgit v1.2.3