summaryrefslogtreecommitdiff
path: root/src/c
diff options
context:
space:
mode:
authorGravatar Adam Chlipala <adamc@hcoop.net>2009-04-26 10:45:59 -0400
committerGravatar Adam Chlipala <adamc@hcoop.net>2009-04-26 10:45:59 -0400
commit7bd2bc26389467f685958601cff2708751764515 (patch)
treed20dcbe8aec266976ec07b31a5e11e75162f4fdf /src/c
parentbc7a09d702e53faaa882bb7b2591abe68ff5648e (diff)
Reading blobs from the database
Diffstat (limited to 'src/c')
-rw-r--r--src/c/driver.c16
-rw-r--r--src/c/urweb.c40
2 files changed, 51 insertions, 5 deletions
diff --git a/src/c/driver.c b/src/c/driver.c
index 63a7d224..2f84184f 100644
--- a/src/c/driver.c
+++ b/src/c/driver.c
@@ -148,7 +148,7 @@ void uw_sign(const char *in, char *out) {
static void *worker(void *data) {
int me = *(int *)data, retries_left = MAX_RETRIES;
uw_context ctx = new_context();
- size_t buf_size = 1;
+ size_t buf_size = 2;
char *buf = malloc(buf_size);
while (1) {
@@ -167,7 +167,7 @@ static void *worker(void *data) {
unsigned retries_left = MAX_RETRIES;
int r;
- if (back - buf == buf_size) {
+ if (back - buf == buf_size - 1) {
char *new_buf;
buf_size *= 2;
new_buf = realloc(buf, buf_size);
@@ -175,7 +175,7 @@ static void *worker(void *data) {
buf = new_buf;
}
- r = recv(sock, back, buf_size - (back - buf), 0);
+ r = recv(sock, back, buf_size - 1 - (back - buf), 0);
if (r < 0) {
fprintf(stderr, "Recv failed\n");
@@ -235,15 +235,21 @@ static void *worker(void *data) {
}
while (back - after_headers < clen) {
- if (back - buf == buf_size) {
+ 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 - (back - buf), 0);
+ r = recv(sock, back, buf_size - 1 - (back - buf), 0);
if (r < 0) {
fprintf(stderr, "Recv failed\n");
diff --git a/src/c/urweb.c b/src/c/urweb.c
index 28364f2c..22b8a902 100644
--- a/src/c/urweb.c
+++ b/src/c/urweb.c
@@ -443,6 +443,11 @@ void uw_set_headers(uw_context ctx, char *headers) {
ctx->headers_end = s;
}
+void uw_headers_moved(uw_context ctx, char *headers) {
+ ctx->headers_end = headers + (ctx->headers_end - ctx->headers);
+ ctx->headers = headers;
+}
+
int uw_db_begin(uw_context);
__attribute__((noreturn)) void uw_error(uw_context ctx, failure_kind fk, const char *fmt, ...) {
@@ -1481,6 +1486,11 @@ uw_Basis_string uw_maybe_strdup(uw_context ctx, uw_Basis_string s1) {
return NULL;
}
+char *uw_memdup(uw_context ctx, const char *p, size_t len) {
+ char *r = uw_malloc(ctx, len);
+ memcpy(r, p, len);
+ return r;
+}
char *uw_Basis_sqlifyInt(uw_context ctx, uw_Basis_int n) {
int len;
@@ -1896,6 +1906,36 @@ uw_Basis_time uw_Basis_stringToTime_error(uw_context ctx, uw_Basis_string s) {
}
}
+uw_Basis_blob uw_Basis_stringToBlob_error(uw_context ctx, uw_Basis_string s, size_t len) {
+ char *r = ctx->heap.front;
+ uw_Basis_blob b = {len, r};
+
+ uw_check_heap(ctx, len);
+
+ while (*s) {
+ if (s[0] == '\\') {
+ if (s[1] == '\\') {
+ *r++ = '\\';
+ s += 2;
+ } else if (isdigit(s[1]) && isdigit(s[2]) && isdigit(s[3])) {
+ *r++ = (s[1] - '0') * 8 * 8 + ((s[2] - '0') * 8) + (s[3] - '0');
+ s += 4;
+ }
+ else {
+ *r++ = '\\';
+ ++s;
+ }
+ } else {
+ *r++ = s[0];
+ ++s;
+ }
+ }
+
+ b.size = r - ctx->heap.front;
+ ctx->heap.front = r;
+ return b;
+}
+
uw_Basis_string uw_Basis_get_cookie(uw_context ctx, uw_Basis_string c) {
int len = strlen(c);
char *s = ctx->headers, *p = ctx->outHeaders.start;