summaryrefslogtreecommitdiff
path: root/httpd
diff options
context:
space:
mode:
authorJeffrey Armstrong <jeffrey.armstrong@approximatrix.com>2016-12-14 09:14:39 -0500
committerJeffrey Armstrong <jeffrey.armstrong@approximatrix.com>2016-12-14 09:14:39 -0500
commitdc65ef038279df55d5e49052eabac4eae2617ee1 (patch)
tree84c7ffc081482c18c2bc0a6ddb1a4d6dd260f925 /httpd
parent84de937689bcb35ea3ddae55d9c74dc23463f79c (diff)
downloaduIP-FOSSIL-dc65ef038279df55d5e49052eabac4eae2617ee1.tar.gz
uIP-FOSSIL-dc65ef038279df55d5e49052eabac4eae2617ee1.zip
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.
Diffstat (limited to 'httpd')
-rw-r--r--httpd/http-str.c15
-rw-r--r--httpd/http-str.h5
-rw-r--r--httpd/httpd-fs.c67
-rw-r--r--httpd/httpd-fs.h4
-rw-r--r--httpd/httpd.c50
-rw-r--r--httpd/httpd.h1
6 files changed, 124 insertions, 18 deletions
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 <stdlib.h>
#include <string.h>
#include <dos.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#include "httpd-fs.h"
-void change_to_backslash(char *name)
-{
-int counter;
- for(counter=0;counter<strlen(name);counter++) {
- if(name[counter]=='/') name[counter] = '\\';
- }
-}
-
void safe_name(char *name)
{
#ifdef WATCOM
@@ -59,6 +53,61 @@ char *newtext;
#endif
}
+void change_to_backslash(char *name)
+{
+int counter;
+ for(counter=0;counter<strlen(name);counter++) {
+ if(name[counter]=='/') name[counter] = '\\';
+ }
+}
+
+int compressed_name(char *str)
+{
+char *ext;
+
+ if(str == NULL)
+ return -1;
+
+ ext = strrchr(str, '.');
+ if(ext == NULL || strlen(ext) < 4)
+ return -1;
+
+ ext[2] = 'g';
+ ext[3] = 'z';
+ return 1;
+}
+
+int compressed_available(const char *name)
+{
+char *cname;
+char *noslash;
+int ret;
+struct stat st;
+
+ if(name == NULL) return -1;
+
+ noslash = name;
+ while(noslash[0] == '/') noslash++;
+ if(noslash[0] == '\0') noslash = "index.htm";
+
+ cname = (char *)malloc((strlen(noslash)+1)*sizeof(char));
+ if(cname == NULL) return -1;
+
+ strcpy(cname, noslash);
+ ret = compressed_name(cname);
+
+ if(ret == 1) {
+ change_to_backslash(cname);
+ safe_name(cname);
+
+ if(stat(cname,&st) < 0)
+ ret = -1;
+ }
+
+ free(cname);
+ return ret;
+}
+
int httpd_fs_open(char *name, struct httpd_fs_file *file)
{
FILE *fp;
diff --git a/httpd/httpd-fs.h b/httpd/httpd-fs.h
index 7fa93ec..cf5a5de 100644
--- a/httpd/httpd-fs.h
+++ b/httpd/httpd-fs.h
@@ -52,4 +52,8 @@ int httpd_fs_open(char *name, struct httpd_fs_file *file);
void httpd_fs_init(void);
+/* Check if a compressed version is available */
+int compressed_name(char *str);
+int compressed_available(const char *name);
+
#endif /* __HTTPD_FS_H__ */
diff --git a/httpd/httpd.c b/httpd/httpd.c
index c7cc49c..459bfa2 100644
--- a/httpd/httpd.c
+++ b/httpd/httpd.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009 Jeffrey Armstrong
+ * Copyright (c) 2009, 2016 Jeffrey Armstrong
* Copyright (c) 2001-2005, Adam Dunkels.
* All rights reserved.
*
@@ -37,6 +37,7 @@
#include "http-str.h"
#include <string.h>
+#include <time.h>
#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(&ltime));
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;
};