comparison src/c/urweb.c @ 741:f7e2026dd5ae

Returning a blob as page result
author Adam Chlipala <adamc@hcoop.net>
date Sun, 26 Apr 2009 09:02:17 -0400
parents b302b6e35f93
children 43553c93dd8c
comparison
equal deleted inserted replaced
740:b302b6e35f93 741:f7e2026dd5ae
1 #define _XOPEN_SOURCE 1 #define _XOPEN_SOURCE 500
2 2
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <stdio.h> 4 #include <stdio.h>
5 #include <string.h> 5 #include <string.h>
6 #include <strings.h> 6 #include <strings.h>
7 #include <ctype.h> 7 #include <ctype.h>
8 #include <setjmp.h> 8 #include <setjmp.h>
9 #include <stdarg.h> 9 #include <stdarg.h>
10 #include <assert.h> 10 #include <assert.h>
11 #include <ctype.h>
11 12
12 #include <pthread.h> 13 #include <pthread.h>
13 14
14 #include "types.h" 15 #include "types.h"
15 16
2102 2103
2103 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) { 2104 uw_Basis_string uw_Basis_bless(uw_context ctx, uw_Basis_string s) {
2104 return s; 2105 return s;
2105 } 2106 }
2106 2107
2108 uw_Basis_string uw_Basis_blessMime(uw_context ctx, uw_Basis_string s) {
2109 char *s2;
2110
2111 for (s2 = s; *s2; ++s2)
2112 if (!isalnum(*s2) && *s2 != '/' && *s2 != '-' && *s2 != '.')
2113 uw_error(ctx, FATAL, "MIME type \"%s\" contains invalid character %c\n", s, *s2);
2114
2115 return s;
2116 }
2117
2107 uw_Basis_string uw_unnull(uw_Basis_string s) { 2118 uw_Basis_string uw_unnull(uw_Basis_string s) {
2108 return s ? s : ""; 2119 return s ? s : "";
2109 } 2120 }
2110 2121
2111 extern int uw_hash_blocksize; 2122 extern int uw_hash_blocksize;
2133 } 2144 }
2134 2145
2135 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) { 2146 uw_Basis_blob uw_Basis_fileData(uw_context ctx, uw_Basis_file f) {
2136 return f.data; 2147 return f.data;
2137 } 2148 }
2149
2150 __attribute__((noreturn)) void uw_return_blob(uw_context ctx, uw_Basis_blob b, uw_Basis_string mimeType) {
2151 cleanup *cl;
2152 int len;
2153
2154 buf_reset(&ctx->outHeaders);
2155 buf_reset(&ctx->page);
2156
2157 uw_write_header(ctx, "HTTP/1.1 200 OK\r\nContent-Type: ");
2158 uw_write_header(ctx, mimeType);
2159 uw_write_header(ctx, "\r\nContent-Length: ");
2160 buf_check(&ctx->outHeaders, INTS_MAX);
2161 sprintf(ctx->outHeaders.front, "%d%n", b.size, &len);
2162 ctx->outHeaders.front += len;
2163 uw_write_header(ctx, "\r\n");
2164
2165 buf_append(&ctx->page, b.data, b.size);
2166
2167 for (cl = ctx->cleanup; cl < ctx->cleanup_front; ++cl)
2168 cl->func(cl->arg);
2169
2170 ctx->cleanup_front = ctx->cleanup;
2171
2172 longjmp(ctx->jmp_buf, RETURN_BLOB);
2173 }