summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2008-07-13 13:38:23 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2008-07-13 13:38:23 -0400
commit0eb51a6e21e5f435d37afd885ae316a0575c7208 (patch)
treeb8d8a99e6a1a518ae6c45ebffff6877985f8537f /src
parent3c2cf28f8c508355be291a3ce2ecc9bb571f97dd (diff)
Basic web server
Diffstat (limited to 'src')
-rw-r--r--src/c/driver.c134
-rw-r--r--src/cjr_print.sml14
-rw-r--r--src/cjrize.sml2
3 files changed, 139 insertions, 11 deletions
diff --git a/src/c/driver.c b/src/c/driver.c
index 51d8bab7..16b2dad5 100644
--- a/src/c/driver.c
+++ b/src/c/driver.c
@@ -1,8 +1,132 @@
-void lw_handle(void);
+#include <stdio.h>
+
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+int lw_port = 8080;
+int lw_backlog = 10;
+int lw_bufsize = 1024;
+
+void lw_handle(char*);
+
+static void worker(int sock) {
+ char buf[lw_bufsize+1], *back = buf, *s;
+
+ while (1) {
+ int r = recv(sock, back, lw_bufsize - (back - buf), 0);
+
+ if (r < 0) {
+ fprintf(stderr, "Recv failed\n");
+ close(sock);
+ return;
+ }
+
+ if (r == 0) {
+ printf("Connection closed.\n");
+ close(sock);
+ return;
+ }
+
+ printf("Received %d bytes.\n", r);
+
+ back += r;
+ *back = 0;
+
+ if (s = strstr(buf, "\r\n\r\n")) {
+ char *cmd, *path;
+
+ *s = 0;
+
+ if (!(s = strstr(buf, "\n"))) {
+ fprintf(stderr, "No newline in buf\n");
+ close(sock);
+ return;
+ }
+
+ *s = 0;
+ cmd = s = buf;
+
+ if (!strsep(&s, " ")) {
+ fprintf(stderr, "No first space in HTTP command\n");
+ close(sock);
+ return;
+ }
+
+ if (strcmp(cmd, "GET")) {
+ fprintf(stderr, "Not ready for non-get command: %s\n", cmd);
+ close(sock);
+ return;
+ }
+
+ path = s;
+ if (!strsep(&s, " ")) {
+ fprintf(stderr, "No second space in HTTP command\n");
+ close(sock);
+ return;
+ }
+
+ printf("Serving URI %s....\n", path);
+ puts("Content-type: text/html\n\n");
+ puts("<html>");
+ lw_handle(path);
+ puts("</html>");
+
+ printf("Done with client.\n\n");
+ close(sock);
+ return;
+ }
+ }
+}
int main() {
- puts("<html>");
- lw_handle();
- puts("</html>");
- return 0;
+ // The skeleton for this function comes from Beej's sockets tutorial.
+ int sockfd, new_fd; // listen on sock_fd, new connection on new_fd
+ struct sockaddr_in my_addr;
+ struct sockaddr_in their_addr; // connector's address information
+ int sin_size, yes = 1;
+
+ sockfd = socket(PF_INET, SOCK_STREAM, 0); // do some error checking!
+
+ if (sockfd < 0) {
+ fprintf(stderr, "Listener socket creation failed\n");
+ return 1;
+ }
+
+ if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) < 0) {
+ fprintf(stderr, "Listener socket option setting failed\n");
+ return 1;
+ }
+
+ my_addr.sin_family = AF_INET; // host byte order
+ my_addr.sin_port = htons(lw_port); // short, network byte order
+ my_addr.sin_addr.s_addr = INADDR_ANY; // auto-fill with my IP
+ memset(my_addr.sin_zero, '\0', sizeof my_addr.sin_zero);
+
+ if (bind(sockfd, (struct sockaddr *)&my_addr, sizeof my_addr) < 0) {
+ fprintf(stderr, "Listener socket bind failed\n");
+ return 1;
+ }
+
+ if (listen(sockfd, lw_backlog) < 0) {
+ fprintf(stderr, "Socket listen failed\n");
+ return 1;
+ }
+
+ sin_size = sizeof their_addr;
+
+ printf("Listening on port %d....\n", lw_port);
+
+ while (1) {
+ new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
+
+ if (new_fd < 0) {
+ fprintf(stderr, "Socket accept failed\n");
+ return 1;
+ }
+
+ printf("Accepted connection.\n");
+ worker(new_fd);
+ }
}
diff --git a/src/cjr_print.sml b/src/cjr_print.sml
index f3beb023..19df046f 100644
--- a/src/cjr_print.sml
+++ b/src/cjr_print.sml
@@ -174,11 +174,15 @@ fun p_decl env ((d, _) : decl) =
end
fun p_page env (s, n) =
- box [string "/* ",
- string s,
- string " */ ",
+ box [string "if (!strcmp(request, \"",
+ string (String.toString s),
+ string "\")) {",
+ newline,
p_enamed env n,
- string "(lw_unit_v);"]
+ string "(lw_unit_v);",
+ newline,
+ string "}",
+ newline]
fun p_file env (ds, ps) =
let
@@ -193,7 +197,7 @@ fun p_file env (ds, ps) =
newline,
p_list_sep newline (fn x => x) pds,
newline,
- string "void lw_handle(void) {",
+ string "void lw_handle(char *request) {",
newline,
p_list_sep newline (fn x => x) pds',
newline,
diff --git a/src/cjrize.sml b/src/cjrize.sml
index 558fac0b..78b73dba 100644
--- a/src/cjrize.sml
+++ b/src/cjrize.sml
@@ -184,7 +184,7 @@ fun cifyDecl ((d, loc), sm) =
in
(SOME (d, loc), NONE, sm)
end
- | L.DExport n => (NONE, SOME n, sm)
+ | L.DExport (s, n) => (NONE, SOME ("/" ^ s, n), sm)
fun cjrize ds =
let