comparison src/c/driver.c @ 464:91914c15a85b

Cookie demo code; fix error message display
author Adam Chlipala <adamc@hcoop.net>
date Thu, 06 Nov 2008 12:22:50 -0500
parents 21bb5bbba2e9
children 1626dcba13ee
comparison
equal deleted inserted replaced
463:bb27c7efcd90 464:91914c15a85b
187 } 187 }
188 188
189 printf("Serving URI %s....\n", path); 189 printf("Serving URI %s....\n", path);
190 190
191 uw_set_headers(ctx, headers); 191 uw_set_headers(ctx, headers);
192 uw_write(ctx, "<html>");
192 193
193 while (1) { 194 while (1) {
194 if (uw_db_begin(ctx)) { 195 if (uw_db_begin(ctx)) {
195 printf("Error running SQL BEGIN\n"); 196 printf("Error running SQL BEGIN\n");
196 if (retries_left) 197 if (retries_left)
197 --retries_left; 198 --retries_left;
198 else { 199 else {
199 fk = FATAL; 200 fk = FATAL;
200 uw_reset(ctx); 201 uw_reset(ctx);
201 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 202 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
202 uw_write(ctx, "Content-type: text/plain\r\n\r\n"); 203 uw_write_header(ctx, "Content-type: text/plain\r\n\r\n");
203 uw_write(ctx, "Error running SQL BEGIN\n"); 204 uw_write(ctx, "Error running SQL BEGIN\n");
204 205
205 break; 206 break;
206 } 207 }
207 } 208 }
210 uw_write_header(ctx, "Content-type: text/html\r\n"); 211 uw_write_header(ctx, "Content-type: text/html\r\n");
211 212
212 strcpy(path_copy, path); 213 strcpy(path_copy, path);
213 fk = uw_begin(ctx, path_copy); 214 fk = uw_begin(ctx, path_copy);
214 if (fk == SUCCESS) { 215 if (fk == SUCCESS) {
216 uw_write(ctx, "</html>");
217
215 if (uw_db_commit(ctx)) { 218 if (uw_db_commit(ctx)) {
216 fk = FATAL; 219 fk = FATAL;
217 220
218 printf("Error running SQL COMMIT\n"); 221 printf("Error running SQL COMMIT\n");
219 uw_reset(ctx); 222 uw_reset(ctx);
220 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 223 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
221 uw_write(ctx, "Content-type: text/plain\r\n\r\n"); 224 uw_write_header(ctx, "Content-type: text/plain\r\n");
222 uw_write(ctx, "Error running SQL COMMIT\n"); 225 uw_write(ctx, "Error running SQL COMMIT\n");
223 } 226 }
224 227
225 break; 228 break;
226 } else if (fk == BOUNDED_RETRY) { 229 } else if (fk == BOUNDED_RETRY) {
230 } 233 }
231 else { 234 else {
232 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx)); 235 printf("Fatal error (out of retries): %s\n", uw_error_message(ctx));
233 236
234 uw_reset_keep_error_message(ctx); 237 uw_reset_keep_error_message(ctx);
235 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 238 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
236 uw_write(ctx, "Content-type: text/plain\r\n\r\n"); 239 uw_write_header(ctx, "Content-type: text/plain\r\n");
237 uw_write(ctx, "Fatal error (out of retries): "); 240 uw_write(ctx, "Fatal error (out of retries): ");
238 uw_write(ctx, uw_error_message(ctx)); 241 uw_write(ctx, uw_error_message(ctx));
239 uw_write(ctx, "\n"); 242 uw_write(ctx, "\n");
240 243
241 try_rollback(ctx); 244 try_rollback(ctx);
246 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx)); 249 printf("Error triggers unlimited retry: %s\n", uw_error_message(ctx));
247 else if (fk == FATAL) { 250 else if (fk == FATAL) {
248 printf("Fatal error: %s\n", uw_error_message(ctx)); 251 printf("Fatal error: %s\n", uw_error_message(ctx));
249 252
250 uw_reset_keep_error_message(ctx); 253 uw_reset_keep_error_message(ctx);
251 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 254 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\r\n");
252 uw_write(ctx, "Content-type: text/plain\r\n\r\n"); 255 uw_write_header(ctx, "Content-type: text/plain\r\n");
253 uw_write(ctx, "Fatal error: "); 256 uw_write(ctx, "Fatal error: ");
254 uw_write(ctx, uw_error_message(ctx)); 257 uw_write(ctx, uw_error_message(ctx));
255 uw_write(ctx, "\n"); 258 uw_write(ctx, "\n");
256 259
257 try_rollback(ctx); 260 try_rollback(ctx);
259 break; 262 break;
260 } else { 263 } else {
261 printf("Unknown uw_handle return code!\n"); 264 printf("Unknown uw_handle return code!\n");
262 265
263 uw_reset_keep_request(ctx); 266 uw_reset_keep_request(ctx);
264 uw_write(ctx, "HTTP/1.1 500 Internal Server Error\n\r"); 267 uw_write_header(ctx, "HTTP/1.1 500 Internal Server Error\n\r");
265 uw_write(ctx, "Content-type: text/plain\r\n\r\n"); 268 uw_write_header(ctx, "Content-type: text/plain\r\n");
266 uw_write(ctx, "Unknown uw_handle return code!\n"); 269 uw_write(ctx, "Unknown uw_handle return code!\n");
267 270
268 try_rollback(ctx); 271 try_rollback(ctx);
269 272
270 break; 273 break;