1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <fcntl.h>
  4 #include <unistd.h>
  5 #include "config.h"
  6 #include "client.h"
  7 #include "server.h"
  8 #include "rh_string.h"
  9 #include "handler.h"
 10 #include "http_request.h"
 11 #include "rh_chunk.h"
 12 
 13 /*
 14  *
 15  * error
 16  *
 17  *
 18  *
 19  */
 20 
 21 #define TEST(_call)     if (0 != _call) return HANDLER_FAILURE
 22 
 23 
 24 
 25 DECLARE_HANDLER_FUNCTION (error,exec)
 26 {
 27         http_request_t          *request;
 28         int                     status;
 29         const rh_buffer_t       *response;
 30         const char              *msg;
 31         size_t                  nmsg;
 32         
 33 #ifdef RHTTPD_DUMP_HANDLER_CALL
 34         printf ("%s(%p)\n", __FUNCTION__, (void*)handler);
 35 #endif
 36 
 37         request = handler->request;
 38         
 39         status = request->header_out.status;
 40         
 41         http_header_clear (&request->header_out);
 42         
 43         rh_chunk_reset (&request->chunk_header);
 44         rh_chunk_reset (&request->chunk_content);
 45         
 46         request->header_out.status = (status ? status : 500);
 47 
 48         response = http_header_get_response (&request->header_out);
 49 
 50         msg = response->data + CONST_LEN("HTTP/1.1 ");
 51         nmsg = response->used - CONST_LEN("HTTP/1.1 ");
 52 
 53 
 54         HTTP_HEADER_KEY_SET_CONST(
 55                         &request->header_out,
 56                         CONTENT_TYPE,
 57                         "text/html", CONST_LEN("text/html") );
 58         
 59         HTTP_HEADER_KEY_SET_CONST(
 60                         &request->header_out,
 61                         CONTENT_LANGUAGE,
 62                         "en", CONST_LEN("en") );
 63 
 64         if (HTTP_METHOD_HEAD == request->header_in.method)
 65                 return HANDLER_STOP;
 66 
 67         /*
 68          *
 69          * content
 70          *
 71          *
 72          */
 73         
 74         TEST( RH_CHUNK_APPEND_CONST (&request->chunk_content,
 75         "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" "
 76         "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"NL
 77         "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\">"NL
 78         "<head>"NL
 79         "<title>") );
 80         
 81         TEST( rh_chunk_append_buffer(&request->chunk_content,
 82                                 (char*)msg, nmsg,
 83                                 NULL, NULL) );
 84         
 85         TEST( RH_CHUNK_APPEND_CONST (&request->chunk_content,
 86         "</title></head><body>"NL
 87         "<h1>") );
 88         
 89         TEST( rh_chunk_append_buffer(&request->chunk_content,
 90                                 (char*)msg, nmsg,
 91                                 NULL, NULL) );
 92         
 93         TEST( RH_CHUNK_APPEND_CONST (&request->chunk_content,
 94         "</h1>"NL
 95         "</body></html>"NL) );
 96         
 97         return HANDLER_STOP;
 98 }
 99 
100 
101 DECLARE_HANDLER_BEGIN (error, ERROR)
102         .DECLARE_HANDLER_NULL   (error, setup),
103         .DECLARE_HANDLER_NULL   (error, init),
104         .DECLARE_HANDLER_NULL   (error, init_global),
105         .DECLARE_HANDLER_NULL   (error, free),
106         .DECLARE_HANDLER_NULL   (error, free_global),
107         .DECLARE_HANDLER_SYMBOL (error, exec),
108 DECLARE_HANDLER_END


syntax highlighted by Code2HTML, v. 0.9.1