Commit 7466e5929837bc43f32ac508bb24564689954818
1 parent
abd69d0a
first very crude, not complete, experimental 304 test implementation
Showing
6 changed files
with
97 additions
and
8 deletions
1 | +2012-02-20 10:10:29 +0100 Georg Hopp | |
2 | + | |
3 | + * first very crude, not complete, experimental 304 test implementation (HEAD, master) | |
4 | + | |
1 | 5 | 2012-02-20 07:55:06 +0100 Georg Hopp |
2 | 6 | |
3 | - * disconnect on invalid request line (HEAD, master) | |
7 | + * disconnect on invalid request line (origin/master, origin/HEAD) | |
4 | 8 | |
5 | 9 | 2012-02-19 20:12:40 +0100 Georg Hopp |
6 | 10 | |
7 | - * now incomplete requests should no longer block the complete server. Tested with \'echo -en "GET / HTTP\r\nConn" | nc -w 600 localhost 11212\' and then doing requests from my browser. @TODO: cleanup those stuff, check if a not correctly response reading would block the server. (origin/master, origin/HEAD) | |
11 | + * now incomplete requests should no longer block the complete server. Tested with \'echo -en "GET / HTTP\r\nConn" | nc -w 600 localhost 11212\' and then doing requests from my browser. @TODO: cleanup those stuff, check if a not correctly response reading would block the server. | |
8 | 12 | |
9 | 13 | 2012-02-19 18:28:30 +0100 Georg Hopp |
10 | 14 | ... | ... |
1 | 1 | #ifndef __HTTP_RESPONSE_H__ |
2 | 2 | #define __HTTP_RESPONSE_H__ |
3 | 3 | |
4 | +#include <time.h> | |
4 | 5 | #include <sys/types.h> |
5 | 6 | |
6 | 7 | #include "class.h" |
... | ... | @@ -14,9 +15,10 @@ CLASS(HttpResponse) { |
14 | 15 | char * reason; |
15 | 16 | }; |
16 | 17 | |
18 | +HttpResponse httpResponse304(int, const char *); | |
17 | 19 | HttpResponse httpResponse404(); |
18 | 20 | HttpResponse httpResponseMe(); |
19 | -HttpResponse httpResponseImage(); | |
21 | +HttpResponse httpResponseImage(int); | |
20 | 22 | |
21 | 23 | //void httpResponseHeaderSet(HttpResponse, const char *, const char *); |
22 | 24 | ... | ... |
... | ... | @@ -20,6 +20,7 @@ MSG = http/message.c http/message/queue.c http/message/has_keep_alive.c \ |
20 | 20 | http/message/get_version.c http/message/has_valid_version.c |
21 | 21 | REQ = http/request.c http/request/has_valid_method.c |
22 | 22 | RESP = http/response.c \ |
23 | + http/response/304.c \ | |
23 | 24 | http/response/404.c \ |
24 | 25 | http/response/image.c \ |
25 | 26 | http/response/me.c | ... | ... |
src/http/response/304.c
0 → 100644
1 | +#include <stdlib.h> | |
2 | +#include <string.h> | |
3 | +#include <stdio.h> | |
4 | +#include <time.h> | |
5 | +#include <sys/stat.h> | |
6 | +#include <fcntl.h> | |
7 | + | |
8 | +#include "class.h" | |
9 | +#include "interface/class.h" | |
10 | + | |
11 | +#include "http/response.h" | |
12 | +#include "http/message.h" | |
13 | +#include "http/header.h" | |
14 | + | |
15 | + | |
16 | +HttpResponse | |
17 | +httpResponse304(int handle, const char * content_type) | |
18 | +{ | |
19 | + time_t t; | |
20 | + struct tm * tmp; | |
21 | + char buffer[200]; | |
22 | + struct stat st; | |
23 | + HttpResponse response; | |
24 | + HttpMessage message; | |
25 | + | |
26 | + fstat(handle, &st); | |
27 | + | |
28 | + response = new(HttpResponse, "HTTP/1.1", 304, "Not Modified"); | |
29 | + message = (HttpMessage)response; | |
30 | + | |
31 | + httpHeaderAdd(&(message->header), | |
32 | + new(HttpHeader, "Content-Type", content_type)); | |
33 | + httpHeaderAdd(&(message->header), | |
34 | + new(HttpHeader, "Server", "testserver")); | |
35 | + | |
36 | + message->type = HTTP_MESSAGE_BUFFERED; | |
37 | + message->nbody = 0; | |
38 | + message->body = NULL; | |
39 | + | |
40 | + tmp = localtime(&(st.st_mtime)); | |
41 | + strftime(buffer, sizeof(buffer), "%s", tmp); | |
42 | + httpHeaderAdd(&(message->header), | |
43 | + new(HttpHeader, "ETag", buffer)); | |
44 | + | |
45 | + tmp = localtime(&(st.st_mtime)); | |
46 | + strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); | |
47 | + httpHeaderAdd(&(message->header), | |
48 | + new(HttpHeader, "Last-Modified", buffer)); | |
49 | + | |
50 | + t = time(NULL); | |
51 | + tmp = localtime(&t); | |
52 | + strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); | |
53 | + httpHeaderAdd(&(message->header), | |
54 | + new(HttpHeader, "Date", buffer)); | |
55 | + | |
56 | + return response; | |
57 | +} | |
58 | + | |
59 | +// vim: set ts=4 sw=4: | ... | ... |
... | ... | @@ -13,13 +13,15 @@ |
13 | 13 | |
14 | 14 | |
15 | 15 | HttpResponse |
16 | -httpResponseImage() | |
16 | +httpResponseImage(int handle) | |
17 | 17 | { |
18 | 18 | char buffer[200]; |
19 | 19 | struct stat st; |
20 | 20 | HttpResponse response; |
21 | 21 | HttpMessage message; |
22 | 22 | |
23 | + fstat(handle, &st); | |
24 | + | |
23 | 25 | response = new(HttpResponse, "HTTP/1.1", 200, "OK"); |
24 | 26 | message = (HttpMessage)response; |
25 | 27 | |
... | ... | @@ -29,14 +31,24 @@ httpResponseImage() |
29 | 31 | new(HttpHeader, "Server", "testserver")); |
30 | 32 | |
31 | 33 | message->type = HTTP_MESSAGE_PIPED; |
32 | - message->handle = open("./assets/waldschrat.jpg", O_RDONLY); | |
33 | - fstat(message->handle, &st); | |
34 | + message->handle = handle; | |
34 | 35 | message->nbody = st.st_size; |
35 | 36 | |
36 | 37 | sprintf(buffer, "%d", message->nbody); |
37 | 38 | httpHeaderAdd(&(message->header), |
38 | 39 | new(HttpHeader, "Content-Length", buffer)); |
39 | 40 | |
41 | + tmp = localtime(&(st.st_mtime)); | |
42 | + strftime(buffer, sizeof(buffer), "%s", tmp); | |
43 | + httpHeaderAdd(&(message->header), | |
44 | + new(HttpHeader, "ETag", buffer)); | |
45 | + | |
46 | + t = time(NULL); | |
47 | + tmp = localtime(&t); | |
48 | + strftime(buffer, sizeof(buffer), "%a, %d %b %Y %T %Z", tmp); | |
49 | + httpHeaderAdd(&(message->header), | |
50 | + new(HttpHeader, "Date", buffer)); | |
51 | + | |
40 | 52 | return response; |
41 | 53 | } |
42 | 54 | ... | ... |
1 | -#include <time.h> | |
1 | +#include <sys/types.h> | |
2 | +#include <sys/stat.h> | |
3 | +#include <fcntl.h> | |
2 | 4 | |
3 | 5 | #include "class.h" |
4 | 6 | #include "interface/class.h" |
... | ... | @@ -36,7 +38,16 @@ httpWorkerProcess(HttpWorker this, int fd) |
36 | 38 | } |
37 | 39 | else if (0 == strcmp("GET", request->method) && |
38 | 40 | 0 == strcmp("/image/", request->uri)) { |
39 | - response = (HttpMessage)httpResponseImage(); | |
41 | + int handle = open("./assets/waldschrat.jpg", O_RDONLY); | |
42 | + | |
43 | + if (NULL != httpHeaderGet( | |
44 | + &(((HttpMessage)request)->header), | |
45 | + "If-None-Match")) { | |
46 | + response = httpResponse304(handle, "image/jpeg"); | |
47 | + } | |
48 | + else { | |
49 | + response = (HttpMessage)httpResponseImage(handle); | |
50 | + } | |
40 | 51 | } |
41 | 52 | else { |
42 | 53 | response = (HttpMessage)httpResponse404(); | ... | ... |
Please
register
or
login
to post a comment