Permalink
Browse files

Make the http server send a content length.

  • Loading branch information...
msullivan committed Mar 9, 2012
1 parent dd376d0 commit a8fc5709bc409893682cfb7976a98a93cc6b9077
Showing with 16 additions and 5 deletions.
  1. +5 −5 src/Http.hs
  2. +10 −0 src/rt/http_lib.c
  3. +1 −0 src/rt/lib.h
View
@@ -9,8 +9,9 @@ bufsize = 4096*4
http_parse :: BufferE -> IntE -> Prog IntE
http_parse buf len =
callName "parse_result" (CFn "http_parse") Int (buf, len)
-
-response_header = "HTTP/1.0 200 OK\r\n\r\n"
+http_make_hdr :: BufferE -> IntE -> FdE -> Prog IntE
+http_make_hdr buf len fd =
+ callName "hdr_length" (CFn "http_make_hdr") Int (buf, len, fd)
setup_connection :: FdE -> Prog (FdE, EventE)
setup_connection fd = do
@@ -50,9 +51,8 @@ child_code = declare_thread (FD) $
ifE' (isFailure file_fd) exit
let cleanup = close file_fd >> exit
- let hdr_length = num $ length response_header
- header <- var "output_header" Buffer (stringLit response_header)
- amount_written <- full_write ev header hdr_length
+ hdr_length <- http_make_hdr buf bufsize file_fd
+ amount_written <- full_write ev buf hdr_length
ifE' (amount_written .< hdr_length) cleanup
while 1 $ do
View
@@ -43,3 +43,13 @@ int http_parse(char *buf, int len)
//printf("opening file: %s\n", buf);
return 1;
}
+
+int http_make_hdr(char *buf, int len, int fd)
+{
+ struct stat sbuf;
+ fstat(fd, &sbuf);
+
+ return snprintf(buf, len,
+ "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Length: %d\r\n\r\n",
+ sbuf.st_size);
+}
View
@@ -38,5 +38,6 @@ int print_int(int n);
// This shouldn't be here. Programs should be able to include other headers
int http_parse(char *buf, int len);
+int http_make_hdr(char *buf, int len, int fd);
#endif

0 comments on commit a8fc570

Please sign in to comment.