summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-06-23 14:05:12 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-06-23 14:05:12 -0400
commit3602f46fee1c01d173177298abd3caa58e3d946b (patch)
treee623aac5c0b1237fc0e7d16fa0749b114d18aa6e
parentc701f11b2ee105af75dbeb4baaf0f2c35bb417e2 (diff)
Factor out common request functionality, in preparation for supporting different protocols
-rw-r--r--Makefile.in9
-rw-r--r--doc/manual.tex2
-rw-r--r--include/request.h22
-rw-r--r--include/types.h2
-rw-r--r--src/c/driver.c481
-rw-r--r--src/c/request.c467
-rw-r--r--src/compiler.sml4
7 files changed, 520 insertions, 467 deletions
diff --git a/Makefile.in b/Makefile.in
index 4f50ec81..9347e96f 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -13,7 +13,7 @@ all: smlnj mlton c
smlnj: src/urweb.cm
mlton: bin/urweb
-c: lib/c/urweb.o lib/c/driver.o
+c: lib/c/urweb.o lib/c/request.o lib/c/driver.o
clean:
rm -f src/*.mlton.grm.* src/*.mlton.lex.* \
@@ -21,11 +21,8 @@ clean:
lib/c/*.o
rm -rf .cm src/.cm
-lib/c/urweb.o: src/c/urweb.c include/*.h
- gcc -O3 -I include -c src/c/urweb.c -o lib/c/urweb.o $(CFLAGS)
-
-lib/c/driver.o: src/c/driver.c include/*.h
- gcc -O3 -I include -c src/c/driver.c -o lib/c/driver.o $(CFLAGS)
+lib/c/%.o: src/c/%.c include/*.h
+ gcc -O3 -I include -c $< -o $@ $(CFLAGS)
src/urweb.cm: src/prefix.cm src/sources
cat src/prefix.cm src/sources \
diff --git a/doc/manual.tex b/doc/manual.tex
index 0964133e..22c219ad 100644
--- a/doc/manual.tex
+++ b/doc/manual.tex
@@ -394,7 +394,7 @@ A signature item $\mt{functor} \; X_1 \; (X_2 : S_1) : S_2$ is elaborated into $
An $\mt{open} \; \mt{constraints}$ declaration is implicitly inserted for the argument of every functor at the beginning of the functor body. For every declaration of the form $\mt{structure} \; X : S = \mt{struct} \ldots \mt{end}$, an $\mt{open} \; \mt{constraints} \; X$ declaration is implicitly inserted immediately afterward.
-A declaration $\mt{table} \; x : \{(c = c,)^*\}$ is elaborated into $\mt{table} \; x : [(c = c,)^*]$
+A declaration $\mt{table} \; x : \{(c = c,)^*\}$ is elaborated into $\mt{table} \; x : [(c = c,)^*]$.
The syntax $\mt{where} \; \mt{type}$ is an alternate form of $\mt{where} \; \mt{con}$.
diff --git a/include/request.h b/include/request.h
new file mode 100644
index 00000000..1111f47f
--- /dev/null
+++ b/include/request.h
@@ -0,0 +1,22 @@
+#ifndef REQUEST_H
+#define REQUEST_H
+
+#include <sys/types.h>
+
+#include "types.h"
+
+typedef struct uw_rc *uw_request_context;
+
+void uw_request_init(void);
+void uw_sign(const char *in, char *out);
+
+uw_request_context uw_new_request_context(void);
+void uw_free_request_context(uw_request_context);
+
+request_result uw_request(uw_request_context, uw_context, char *request, size_t request_len, int sock);
+
+uw_context uw_request_new_context(void);
+
+void *client_pruner(void *data);
+
+#endif
diff --git a/include/types.h b/include/types.h
index ca9ef152..4a28452b 100644
--- a/include/types.h
+++ b/include/types.h
@@ -40,6 +40,8 @@ typedef struct uw_Basis_file {
typedef enum { SUCCESS, FATAL, BOUNDED_RETRY, UNLIMITED_RETRY, RETURN_BLOB } failure_kind;
+typedef enum { SERVED, KEEP_OPEN, FAILED } request_result;
+
typedef struct input *uw_input;
#define INTS_MAX 50
diff --git a/src/c/driver.c b/src/c/driver.c
index 2140cb2c..56eba9a6 100644
--- a/src/c/driver.c
+++ b/src/c/driver.c
@@ -14,9 +14,9 @@
#include <mhash.h>
#include "urweb.h"
+#include "request.h"
int uw_backlog = 10;
-int uw_bufsize = 1024;
typedef struct node {
int fd;
@@ -54,108 +54,16 @@ static int dequeue() {
static pthread_mutex_t queue_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t queue_cond = PTHREAD_COND_INITIALIZER;
-#define MAX_RETRIES 5
-
-static int try_rollback(uw_context ctx) {
- int r = uw_rollback(ctx);
-
- if (r) {
- printf("Error running SQL ROLLBACK\n");
- uw_reset(ctx);
- uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
- uw_write(ctx, "Content-type: text/plain\r\n\r\n");
- uw_write(ctx, "Error running SQL ROLLBACK\n");
- }
-
- return r;
-}
-
-static uw_context new_context() {
- uw_context ctx = uw_init();
- int retries_left = MAX_RETRIES;
-
- while (1) {
- failure_kind fk = uw_begin_init(ctx);
-
- if (fk == SUCCESS) {
- printf("Database connection initialized.\n");
- break;
- } else if (fk == BOUNDED_RETRY) {
- if (retries_left) {
- printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
- --retries_left;
- } else {
- printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
- uw_free(ctx);
- return NULL;
- }
- } else if (fk == UNLIMITED_RETRY)
- printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
- else if (fk == FATAL) {
- printf("Fatal initialization error: %s\n", uw_error_message(ctx));
- uw_free(ctx);
- return NULL;
- } else {
- printf("Unknown uw_begin_init return code!\n");
- uw_free(ctx);
- return NULL;
- }
- }
-
- return ctx;
-}
-
-#define KEYSIZE 16
-#define PASSSIZE 4
-
-#define HASH_ALGORITHM MHASH_SHA256
-#define HASH_BLOCKSIZE 32
-#define KEYGEN_ALGORITHM KEYGEN_MCRYPT
-
-int uw_hash_blocksize = HASH_BLOCKSIZE;
-
-static int password[PASSSIZE];
-static unsigned char private_key[KEYSIZE];
-
-static void init_crypto() {
- KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
- int i;
-
- assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
-
- for (i = 0; i < PASSSIZE; ++i)
- password[i] = rand();
-
- if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
- private_key, sizeof(private_key),
- (unsigned char*)password, sizeof(password)) < 0) {
- printf("Key generation failed\n");
- exit(1);
- }
-}
-
-void uw_sign(const char *in, char *out) {
- MHASH td;
-
- td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
- mhash_get_hash_pblock(HASH_ALGORITHM));
-
- mhash(td, in, strlen(in));
- if (mhash_hmac_deinit(td, out) < 0)
- printf("Signing failed");
-}
-
static void *worker(void *data) {
- int me = *(int *)data, retries_left = MAX_RETRIES;
- uw_context ctx = new_context();
+ int me = *(int *)data;
+ uw_context ctx = uw_request_new_context();
size_t buf_size = 2;
char *buf = malloc(buf_size);
- size_t path_copy_size = 0;
- char *path_copy = malloc(path_copy_size);
+ uw_request_context rc = uw_new_request_context();
while (1) {
- char *back = buf, *s, *post;
- int sock, dont_close = 0;
+ char *back = buf;
+ int sock;
pthread_mutex_lock(&queue_mutex);
while (empty())
@@ -166,8 +74,8 @@ static void *worker(void *data) {
printf("Handling connection with thread #%d.\n", me);
while (1) {
- unsigned retries_left = MAX_RETRIES;
int r;
+ char *s1, *s2;
if (back - buf == buf_size - 1) {
char *new_buf;
@@ -189,358 +97,40 @@ static void *worker(void *data) {
break;
}
- //printf("Received %d bytes.\n", r);
-
back += r;
*back = 0;
- if (s = strstr(buf, "\r\n\r\n")) {
- failure_kind fk;
- int is_post = 0, do_normal_send = 1;
- char *boundary = NULL;
- size_t boundary_len;
- char *cmd, *path, *headers, *inputs, *after_headers;
-
- //printf("All: %s\n", buf);
-
- s[2] = 0;
- after_headers = s + 4;
-
- if (!(s = strstr(buf, "\r\n"))) {
- fprintf(stderr, "No newline in buf\n");
- break;
- }
-
- *s = 0;
- headers = s + 2;
- cmd = s = buf;
-
- //printf("Read: %s\n", buf);
-
- if (!strsep(&s, " ")) {
- fprintf(stderr, "No first space in HTTP command\n");
- break;
- }
-
- uw_set_headers(ctx, headers);
-
- if (!strcmp(cmd, "POST")) {
- char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
- if (!clen_s) {
- fprintf(stderr, "No Content-length with POST\n");
- goto done;
- }
- int clen = atoi(clen_s);
- if (clen < 0) {
- fprintf(stderr, "Negative Content-length with POST\n");
- goto done;
- }
-
- while (back - after_headers < clen) {
- if (back - buf == buf_size - 1) {
- char *new_buf;
- buf_size *= 2;
- new_buf = realloc(buf, buf_size);
-
- back = new_buf + (back - buf);
- headers = new_buf + (headers - buf);
- uw_headers_moved(ctx, headers);
- after_headers = new_buf + (after_headers - buf);
- s = new_buf + (s - buf);
-
- buf = new_buf;
- }
-
- r = recv(sock, back, buf_size - 1 - (back - buf), 0);
-
- if (r < 0) {
- fprintf(stderr, "Recv failed\n");
- goto done;
- }
-
- if (r == 0) {
- printf("Connection closed.\n");
- goto done;
- }
-
- back += r;
- *back = 0;
- }
+ if ((s1 = strstr(buf, "\r\n\r\n"))) {
+ request_result rr;
- is_post = 1;
+ if ((s2 = strcasestr(buf, "\r\nContent-Length: ")) && s2 < s1) {
+ int clen;
- clen_s = uw_Basis_requestHeader(ctx, "Content-type");
- if (clen_s && !strncasecmp(clen_s, "multipart/form-data", 19)) {
- if (strncasecmp(clen_s + 19, "; boundary=", 11)) {
- fprintf(stderr, "Bad multipart boundary spec");
- break;
- }
-
- boundary = clen_s + 28;
- boundary[0] = '-';
- boundary[1] = '-';
- boundary_len = strlen(boundary);
- }
- } else if (strcmp(cmd, "GET")) {
- fprintf(stderr, "Not ready for non-GET/POST command: %s\n", cmd);
- break;
- }
-
- path = s;
- if (!strsep(&s, " ")) {
- fprintf(stderr, "No second space in HTTP command\n");
- break;
- }
-
- if (!strcmp(path, "/.msgs")) {
- char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
- char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
-
- if (id && pass) {
- unsigned idn = atoi(id);
- uw_client_connect(idn, atoi(pass), sock);
- dont_close = 1;
- fprintf(stderr, "Processed request for messages by client %u\n\n", idn);
- }
- else {
- fprintf(stderr, "Missing fields in .msgs request: %s, %s\n\n", id, pass);
- }
- break;
- }
-
- if (boundary) {
- char *part = after_headers, *after_sub_headers, *header, *after_header;
- size_t part_len;
-
- part = strstr(part, boundary);
- if (!part) {
- fprintf(stderr, "Missing first multipart boundary\n");
+ if (sscanf(s2 + 18, "%d\r\n", &clen) != 1) {
+ fprintf(stderr, "Malformed Content-Length header\n");
break;
}
- part += boundary_len;
-
- while (1) {
- char *name = NULL, *filename = NULL, *type = NULL;
-
- if (part[0] == '-' && part[1] == '-')
- break;
-
- if (*part != '\r') {
- fprintf(stderr, "No \\r after multipart boundary\n");
- goto done;
- }
- ++part;
- if (*part != '\n') {
- fprintf(stderr, "No \\n after multipart boundary\n");
- goto done;
- }
- ++part;
-
- if (!(after_sub_headers = strstr(part, "\r\n\r\n"))) {
- fprintf(stderr, "Missing end of headers after multipart boundary\n");
- goto done;
- }
- after_sub_headers[2] = 0;
- after_sub_headers += 4;
-
- for (header = part; after_header = strstr(header, "\r\n"); header = after_header + 2) {
- char *colon, *after_colon;
-
- *after_header = 0;
- if (!(colon = strchr(header, ':'))) {
- fprintf(stderr, "Missing colon in multipart sub-header\n");
- goto done;
- }
- *colon++ = 0;
- if (*colon++ != ' ') {
- fprintf(stderr, "No space after colon in multipart sub-header\n");
- goto done;
- }
-
- if (!strcasecmp(header, "Content-Disposition")) {
- if (strncmp(colon, "form-data; ", 11)) {
- fprintf(stderr, "Multipart data is not \"form-data\"\n");
- goto done;
- }
-
- for (colon += 11; after_colon = strchr(colon, '='); colon = after_colon) {
- char *data;
- after_colon[0] = 0;
- if (after_colon[1] != '"') {
- fprintf(stderr, "Disposition setting is missing initial quote\n");
- goto done;
- }
- data = after_colon+2;
- if (!(after_colon = strchr(data, '"'))) {
- fprintf(stderr, "Disposition setting is missing final quote\n");
- goto done;
- }
- after_colon[0] = 0;
- ++after_colon;
- if (after_colon[0] == ';' && after_colon[1] == ' ')
- after_colon += 2;
-
- if (!strcasecmp(colon, "name"))
- name = data;
- else if (!strcasecmp(colon, "filename"))
- filename = data;
- }
- } else if (!strcasecmp(header, "Content-Type")) {
- type = colon;
- }
- }
-
- part = memmem(after_sub_headers, back - after_sub_headers, boundary, boundary_len);
- if (!part) {
- fprintf(stderr, "Missing boundary after multipart payload\n");
- goto done;
- }
- part[-2] = 0;
- part_len = part - after_sub_headers - 2;
- part[0] = 0;
- part += boundary_len;
-
- if (filename) {
- uw_Basis_file f = {filename, type, {part_len, after_sub_headers}};
-
- if (uw_set_file_input(ctx, name, f)) {
- puts(uw_error_message(ctx));
- goto done;
- }
- } else if (uw_set_input(ctx, name, after_sub_headers)) {
- puts(uw_error_message(ctx));
- goto done;
- }
- }
- }
- else {
- if (is_post)
- inputs = after_headers;
- else if (inputs = strchr(path, '?'))
- *inputs++ = 0;
-
- if (inputs) {
- char *name, *value;
-
- while (*inputs) {
- name = inputs;
- if (inputs = strchr(inputs, '&'))
- *inputs++ = 0;
- else
- inputs = strchr(name, 0);
-
- if (value = strchr(name, '=')) {
- *value++ = 0;
- if (uw_set_input(ctx, name, value)) {
- puts(uw_error_message(ctx));
- goto done;
- }
- }
- else if (uw_set_input(ctx, name, "")) {
- puts(uw_error_message(ctx));
- goto done;
- }
- }
- }
- }
-
- printf("Serving URI %s....\n", path);
-
- while (1) {
- size_t path_len = strlen(path);
-
- uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
-
- if (path_len + 1 > path_copy_size) {
- path_copy_size = path_len + 1;
- path_copy = realloc(path_copy, path_copy_size);
- }
- strcpy(path_copy, path);
- fk = uw_begin(ctx, path_copy);
- if (fk == SUCCESS || fk == RETURN_BLOB) {
- uw_commit(ctx);
- break;
- } else if (fk == BOUNDED_RETRY) {
- if (retries_left) {
- printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
- --retries_left;
- }
- else {
- printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
-
- try_rollback(ctx);
-
- uw_reset_keep_error_message(ctx);
- uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
- uw_write_header(ctx, "Content-type: text/plain\r\n");
- uw_write(ctx, "Fatal error (out of retries): ");
- uw_write(ctx, uw_error_message(ctx));
- uw_write(ctx, "\n");
-
- break;
- }
- } else if (fk == UNLIMITED_RETRY)
- printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
- else if (fk == FATAL) {
- printf("Fatal error: %s\n", uw_error_message(ctx));
-
- try_rollback(ctx);
-
- uw_reset_keep_error_message(ctx);
- uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
- uw_write_header(ctx, "Content-type: text/html\r\n");
- uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
- uw_write(ctx, "Fatal error: ");
- uw_write(ctx, uw_error_message(ctx));
- uw_write(ctx, "\n</body></html>");
-
- break;
- } else {
- printf("Unknown uw_handle return code!\n");
-
- try_rollback(ctx);
- uw_reset_keep_request(ctx);
- uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
- uw_write_header(ctx, "Content-type: text/plain\r\n");
- uw_write(ctx, "Unknown uw_handle return code!\n");
-
- break;
- }
-
- if (try_rollback(ctx))
- break;
-
- uw_reset_keep_request(ctx);
+ if (s1 + 4 + clen > back)
+ continue;
}
+ rr = uw_request(rc, ctx, buf, back - buf, sock);
uw_send(ctx, sock);
- printf("Done with client.\n\n");
- uw_memstats(ctx);
+ if (rr == SERVED || rr == FAILED)
+ close(sock);
+ else if (rr != KEEP_OPEN)
+ fprintf(stderr, "Illegal uw_request return code: %d\n", rr);
+
break;
}
}
- done:
- if (!dont_close)
- close(sock);
uw_reset(ctx);
}
}
-static void *client_pruner(void *data) {
- uw_context ctx = new_context();
-
- if (!ctx)
- exit(1);
-
- while (1) {
- uw_prune_clients(ctx);
- sleep(5);
- }
-}
-
static void help(char *cmd) {
printf("Usage: %s [-p <port>] [-t <thread-count>]\n", cmd);
}
@@ -550,32 +140,6 @@ static void sigint(int signum) {
exit(0);
}
-static void initialize() {
- uw_context ctx;
- failure_kind fk;
-
- init_crypto();
-
- ctx = new_context();
-
- if (!ctx)
- exit(1);
-
- for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
- printf("Unlimited retry during init: %s\n", uw_error_message(ctx));
- uw_db_rollback(ctx);
- uw_reset(ctx);
- }
-
- if (fk != SUCCESS) {
- printf("Failed to initialize database! %s\n", uw_error_message(ctx));
- uw_db_rollback(ctx);
- exit(1);
- }
-
- uw_free(ctx);
-}
-
int main(int argc, char *argv[]) {
// The skeleton for this function comes from Beej's sockets tutorial.
int sockfd; // listen on sock_fd
@@ -622,8 +186,7 @@ int main(int argc, char *argv[]) {
}
}
- uw_global_init();
- initialize();
+ uw_request_init();
names = calloc(nthreads, sizeof(int));
diff --git a/src/c/request.c b/src/c/request.c
new file mode 100644
index 00000000..b13c6118
--- /dev/null
+++ b/src/c/request.c
@@ -0,0 +1,467 @@
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <unistd.h>
+#include <signal.h>
+
+#include <pthread.h>
+
+#include <mhash.h>
+
+#include "urweb.h"
+
+#define MAX_RETRIES 5
+
+static int try_rollback(uw_context ctx) {
+ int r = uw_rollback(ctx);
+
+ if (r) {
+ printf("Error running SQL ROLLBACK\n");
+ uw_reset(ctx);
+ uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
+ uw_write(ctx, "Content-type: text/plain\r\n\r\n");
+ uw_write(ctx, "Error running SQL ROLLBACK\n");
+ }
+
+ return r;
+}
+
+uw_context uw_request_new_context() {
+ uw_context ctx = uw_init();
+ int retries_left = MAX_RETRIES;
+
+ while (1) {
+ failure_kind fk = uw_begin_init(ctx);
+
+ if (fk == SUCCESS) {
+ printf("Database connection initialized.\n");
+ break;
+ } else if (fk == BOUNDED_RETRY) {
+ if (retries_left) {
+ printf("Initialization error triggers bounded retry: %s\n", uw_error_message(ctx));
+ --retries_left;
+ } else {
+ printf("Fatal initialization error (out of retries): %s\n", uw_error_message(ctx));
+ uw_free(ctx);
+ return NULL;
+ }
+ } else if (fk == UNLIMITED_RETRY)
+ printf("Initialization error triggers unlimited retry: %s\n", uw_error_message(ctx));
+ else if (fk == FATAL) {
+ printf("Fatal initialization error: %s\n", uw_error_message(ctx));
+ uw_free(ctx);
+ return NULL;
+ } else {
+ printf("Unknown uw_begin_init return code!\n");
+ uw_free(ctx);
+ return NULL;
+ }
+ }
+
+ return ctx;
+}
+
+#define KEYSIZE 16
+#define PASSSIZE 4
+
+#define HASH_ALGORITHM MHASH_SHA256
+#define HASH_BLOCKSIZE 32
+#define KEYGEN_ALGORITHM KEYGEN_MCRYPT
+
+int uw_hash_blocksize = HASH_BLOCKSIZE;
+
+static int password[PASSSIZE];
+static unsigned char private_key[KEYSIZE];
+
+static void init_crypto() {
+ KEYGEN kg = {{HASH_ALGORITHM, HASH_ALGORITHM}};
+ int i;
+
+ assert(mhash_get_block_size(HASH_ALGORITHM) == HASH_BLOCKSIZE);
+
+ for (i = 0; i < PASSSIZE; ++i)
+ password[i] = rand();
+
+ if (mhash_keygen_ext(KEYGEN_ALGORITHM, kg,
+ private_key, sizeof(private_key),
+ (unsigned char*)password, sizeof(password)) < 0) {
+ printf("Key generation failed\n");
+ exit(1);
+ }
+}
+
+void uw_request_init() {
+ uw_context ctx;
+ failure_kind fk;
+
+ uw_global_init();
+
+ ctx = uw_request_new_context();
+
+ if (!ctx)
+ exit(1);
+
+ for (fk = uw_initialize(ctx); fk == UNLIMITED_RETRY; fk = uw_initialize(ctx)) {
+ printf("Unlimited retry during init: %s\n", uw_error_message(ctx));
+ uw_db_rollback(ctx);
+ uw_reset(ctx);
+ }
+
+ if (fk != SUCCESS) {
+ printf("Failed to initialize database! %s\n", uw_error_message(ctx));
+ uw_db_rollback(ctx);
+ exit(1);
+ }
+
+ uw_free(ctx);
+
+ init_crypto();
+}
+
+void uw_sign(const char *in, char *out) {
+ MHASH td;
+
+ td = mhash_hmac_init(HASH_ALGORITHM, private_key, sizeof(private_key),
+ mhash_get_hash_pblock(HASH_ALGORITHM));
+
+ mhash(td, in, strlen(in));
+ if (mhash_hmac_deinit(td, out) < 0)
+ printf("Signing failed");
+}
+
+typedef struct uw_rc {
+ size_t path_copy_size;
+ char *path_copy;
+} *uw_request_context;
+
+uw_request_context uw_new_request_context(void) {
+ uw_request_context r = malloc(sizeof(struct uw_rc));
+ r->path_copy_size = 0;
+ r->path_copy = malloc(0);
+ return r;
+}
+
+void uw_free_request_context(uw_request_context r) {
+ free(r->path_copy);
+ free(r);
+}
+
+request_result uw_request(uw_request_context rc, uw_context ctx, char *request, size_t request_len, int sock) {
+ int retries_left = MAX_RETRIES;
+ char *s;
+ failure_kind fk;
+ int is_post = 0, do_normal_send = 1;
+ char *boundary = NULL;
+ size_t boundary_len;
+ char *cmd, *path, *headers, *inputs, *after_headers;
+
+ if (!(s = strstr(request, "\r\n\r\n"))) {
+ fprintf(stderr, "No end of headers found in request\n");
+ return FAILED;
+ }
+
+ s[2] = 0;
+ after_headers = s + 4;
+
+ if (!(s = strstr(request, "\r\n"))) {
+ fprintf(stderr, "No newline in request\n");
+ return FAILED;
+ }
+
+ *s = 0;
+ headers = s + 2;
+ cmd = s = request;
+
+ if (!strsep(&s, " ")) {
+ fprintf(stderr, "No first space in HTTP command\n");
+ return FAILED;
+ }
+
+ uw_set_headers(ctx, headers);
+
+ if (!strcmp(cmd, "POST")) {
+ char *clen_s = uw_Basis_requestHeader(ctx, "Content-length");
+ if (!clen_s) {
+ fprintf(stderr, "No Content-length with POST\n");
+ return FAILED;
+ }
+ int clen = atoi(clen_s);
+ if (clen < 0) {
+ fprintf(stderr, "Negative Content-length with POST\n");
+ return FAILED;
+ }
+
+ if (request + request_len - after_headers < clen) {
+ fprintf(stderr, "Request doesn't contain all POST data (according to Content-Length)\n");
+ return FAILED;
+ }
+
+ is_post = 1;
+
+ clen_s = uw_Basis_requestHeader(ctx, "Content-type");
+ if (clen_s && !strncasecmp(clen_s, "multipart/form-data", 19)) {
+ if (strncasecmp(clen_s + 19, "; boundary=", 11)) {
+ fprintf(stderr, "Bad multipart boundary spec");
+ return FAILED;
+ }
+
+ boundary = clen_s + 28;
+ boundary[0] = '-';
+ boundary[1] = '-';
+ boundary_len = strlen(boundary);
+ }
+ } else if (strcmp(cmd, "GET")) {
+ fprintf(stderr, "Not ready for non-GET/POST command: %s\n", cmd);
+ return FAILED;
+ }
+
+ path = s;
+ if (!strsep(&s, " ")) {
+ fprintf(stderr, "No second space in HTTP command\n");
+ return FAILED;
+ }
+
+ if (!strcmp(path, "/.msgs")) {
+ char *id = uw_Basis_requestHeader(ctx, "UrWeb-Client");
+ char *pass = uw_Basis_requestHeader(ctx, "UrWeb-Pass");
+
+ if (sock < 0) {
+ fprintf(stderr, ".msgs requested, but not socket supplied\n");
+ return FAILED;
+ }
+
+ if (id && pass) {
+ unsigned idn = atoi(id);
+ uw_client_connect(idn, atoi(pass), sock);
+ fprintf(stderr, "Processed request for messages by client %u\n\n", idn);
+ return KEEP_OPEN;
+ }
+ else {
+ fprintf(stderr, "Missing fields in .msgs request: %s, %s\n\n", id, pass);
+ return FAILED;
+ }
+ }
+
+ if (boundary) {
+ char *part = after_headers, *after_sub_headers, *header, *after_header;
+ size_t part_len;
+
+ part = strstr(part, boundary);
+ if (!part) {
+ fprintf(stderr, "Missing first multipart boundary\n");
+ return FAILED;
+ }
+ part += boundary_len;
+
+ while (1) {
+ char *name = NULL, *filename = NULL, *type = NULL;
+
+ if (part[0] == '-' && part[1] == '-')
+ break;
+
+ if (*part != '\r') {
+ fprintf(stderr, "No \\r after multipart boundary\n");
+ return FAILED;
+ }
+ ++part;
+ if (*part != '\n') {
+ fprintf(stderr, "No \\n after multipart boundary\n");
+ return FAILED;
+ }
+ ++part;
+
+ if (!(after_sub_headers = strstr(part, "\r\n\r\n"))) {
+ fprintf(stderr, "Missing end of headers after multipart boundary\n");
+ return FAILED;
+ }
+ after_sub_headers[2] = 0;
+ after_sub_headers += 4;
+
+ for (header = part; after_header = strstr(header, "\r\n"); header = after_header + 2) {
+ char *colon, *after_colon;
+
+ *after_header = 0;
+ if (!(colon = strchr(header, ':'))) {
+ fprintf(stderr, "Missing colon in multipart sub-header\n");
+ return FAILED;
+ }
+ *colon++ = 0;
+ if (*colon++ != ' ') {
+ fprintf(stderr, "No space after colon in multipart sub-header\n");
+ return FAILED;
+ }
+
+ if (!strcasecmp(header, "Content-Disposition")) {
+ if (strncmp(colon, "form-data; ", 11)) {
+ fprintf(stderr, "Multipart data is not \"form-data\"\n");
+ return FAILED;
+ }
+
+ for (colon += 11; after_colon = strchr(colon, '='); colon = after_colon) {
+ char *data;
+ after_colon[0] = 0;
+ if (after_colon[1] != '"') {
+ fprintf(stderr, "Disposition setting is missing initial quote\n");
+ return FAILED;
+ }
+ data = after_colon+2;
+ if (!(after_colon = strchr(data, '"'))) {
+ fprintf(stderr, "Disposition setting is missing final quote\n");
+ return FAILED;
+ }
+ after_colon[0] = 0;
+ ++after_colon;
+ if (after_colon[0] == ';' && after_colon[1] == ' ')
+ after_colon += 2;
+
+ if (!strcasecmp(colon, "name"))
+ name = data;
+ else if (!strcasecmp(colon, "filename"))
+ filename = data;
+ }
+ } else if (!strcasecmp(header, "Content-Type")) {
+ type = colon;
+ }
+ }
+
+ part = memmem(after_sub_headers, request + request_len - after_sub_headers, boundary, boundary_len);
+ if (!part) {
+ fprintf(stderr, "Missing boundary after multipart payload\n");
+ return FAILED;
+ }
+ part[-2] = 0;
+ part_len = part - after_sub_headers - 2;
+ part[0] = 0;
+ part += boundary_len;
+
+ if (filename) {
+ uw_Basis_file f = {filename, type, {part_len, after_sub_headers}};
+
+ if (uw_set_file_input(ctx, name, f)) {
+ fprintf(stderr, "%s\n", uw_error_message(ctx));
+ return FAILED;
+ }
+ } else if (uw_set_input(ctx, name, after_sub_headers)) {
+ fprintf(stderr, "%s\n", uw_error_message(ctx));
+ return FAILED;
+ }
+ }
+ }
+ else {
+ if (is_post)
+ inputs = after_headers;
+ else if (inputs = strchr(path, '?'))
+ *inputs++ = 0;
+
+ if (inputs) {
+ char *name, *value;
+
+ while (*inputs) {
+ name = inputs;
+ if (inputs = strchr(inputs, '&'))
+ *inputs++ = 0;
+ else
+ inputs = strchr(name, 0);
+
+ if (value = strchr(name, '=')) {
+ *value++ = 0;
+ if (uw_set_input(ctx, name, value)) {
+ fprintf(stderr, "%s\n", uw_error_message(ctx));
+ return FAILED;
+ }
+ }
+ else if (uw_set_input(ctx, name, "")) {
+ fprintf(stderr, "%s\n", uw_error_message(ctx));
+ return FAILED;
+ }
+ }
+ }
+ }
+
+ printf("Serving URI %s....\n", path);
+
+ while (1) {
+ size_t path_len = strlen(path);
+
+ uw_write_header(ctx, "HTTP/1.1 200 OK\r\n");
+
+ if (path_len + 1 > rc->path_copy_size) {
+ rc->path_copy_size = path_len + 1;
+ rc->path_copy = realloc(rc->path_copy, rc->path_copy_size);
+ }
+ strcpy(rc->path_copy, path);
+ fk = uw_begin(ctx, rc->path_copy);
+ if (fk == SUCCESS || fk == RETURN_BLOB) {
+ uw_commit(ctx);
+ return SERVED;
+ } else if (fk == BOUNDED_RETRY) {
+ if (retries_left) {
+ printf("Error triggers bounded retry: %s\n", uw_error_message(ctx));
+ --retries_left;
+ }
+ else {
+ printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
+
+ try_rollback(ctx);
+
+ uw_reset_keep_error_message(ctx);
+ uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
+ uw_write_header(ctx, "Content-type: text/plain\r\n");
+ uw_write(ctx, "Fatal error (out of retries): ");
+ uw_write(ctx, uw_error_message(ctx));
+ uw_write(ctx, "\n");
+
+ return FAILED;
+ }
+ } else if (fk == UNLIMITED_RETRY)
+ printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
+ else if (fk == FATAL) {
+ printf("Fatal error: %s\n", uw_error_message(ctx));
+
+ try_rollback(ctx);
+
+ uw_reset_keep_error_message(ctx);
+ uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
+ uw_write_header(ctx, "Content-type: text/html\r\n");
+ uw_write(ctx, "<html><head><title>Fatal Error</title></head><body>");
+ uw_write(ctx, "Fatal error: ");
+ uw_write(ctx, uw_error_message(ctx));
+ uw_write(ctx, "\n</body></html>");
+
+ return FAILED;
+ } else {
+ printf("Unknown uw_handle return code!\n");
+
+ try_rollback(ctx);
+
+ uw_reset_keep_request(ctx);
+ uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
+ uw_write_header(ctx, "Content-type: text/plain\r\n");
+ uw_write(ctx, "Unknown uw_handle return code!\n");
+
+ return FAILED;
+ }
+
+ if (try_rollback(ctx))
+ return FAILED;
+
+ uw_reset_keep_request(ctx);
+ }
+}
+
+void *client_pruner(void *data) {
+ uw_context ctx = uw_request_new_context();
+
+ if (!ctx)
+ exit(1);
+
+ while (1) {
+ uw_prune_clients(ctx);
+ sleep(5);
+ }
+}
diff --git a/src/compiler.sml b/src/compiler.sml
index 4209426f..c7c2f65e 100644
--- a/src/compiler.sml
+++ b/src/compiler.sml
@@ -884,11 +884,13 @@ val toSqlify = transform sqlify "sqlify" o toMono_opt2
fun compileC {cname, oname, ename, libs, profile, debug, link = link'} =
let
val urweb_o = clibFile "urweb.o"
+ val request_o = clibFile "request.o"
val driver_o = clibFile "driver.o"
val compile = "gcc " ^ Config.gccArgs ^ " -Wstrict-prototypes -Werror -O3 -I " ^ Config.includ
^ " -c " ^ cname ^ " -o " ^ oname
- val link = "gcc -Werror -O3 -lm -lmhash -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname ^ " " ^ driver_o ^ " -o " ^ ename
+ val link = "gcc -Werror -O3 -lm -lmhash -pthread " ^ libs ^ " " ^ urweb_o ^ " " ^ oname
+ ^ " " ^ request_o ^ " " ^ driver_o ^ " -o " ^ ename
val (compile, link) =
if profile then