Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

wwwhandler.h

Go to the documentation of this file.
00001 
00002 /*
00003 ** Copyright (C) 2005 Russell Leighton
00004 **
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 **
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 **
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 */
00019 
00020 
00021 
00022 
00023 #ifndef __AWHANDLER_WWW__
00024 #define __AWHANDLER_WWW__
00025 
00026 #include "monitor.h"
00027 
00028 #include "trie.h"
00029 
00031 /*\@{*/
00032 
00058 #ifndef AW_WWWHANDLER_SERVER
00059 #define AW_WWWHANDLER_SERVER "wwwhandler/Aware" AW_VERSION
00060 #endif
00061 
00062 /* safeguard against out of control cgi programs */
00063 #define AW_WWWHANDLER_CGIPAGE_MAXSIZE (10 * (1024 * 1024))
00064 
00068 typedef enum {
00069 
00070   AW_WWWHANDLER_FLAG_NOFILEOPS = 0x1,  
00072 } aw_wwwhandler_flags_t;
00073 
00074 
00078 typedef enum {
00079 
00080   AW_WWWHANDLER_XTN_HEAD,
00081   AW_WWWHANDLER_XTN_GET,
00082 
00083 } aw_wwwhandler_xtn_type_cd_t;
00084 
00085 
00089 typedef enum {
00090 
00091   AW_WWWHANDLER_XTN_WAIT_REQUEST,          
00092   AW_WWWHANDLER_XTN_WAIT_HDR_REPLY,        
00093   AW_WWWHANDLER_XTN_WAIT_SND_REPLY,        
00094   AW_WWWHANDLER_XTN_DONE,                  
00096 } aw_wwwhandler_xtn_state_cd_t;
00097 
00098 /* internal io return codes */
00099 typedef enum {
00100 
00101   IO_OK,
00102   IO_CONT,
00103   IO_ERR,
00104   IO_KEEP_ALIVE_CLOSE
00105 
00106 } aw_wwwhandler_io_code_t;
00107 
00108 
00112 typedef struct {
00113 
00114   /* used on write */
00115   u_int64_t
00116    writesize;
00117 
00118   /* fd for file we are dumping */
00119   int32_t
00120    fd;
00121 
00122   /* ptr for data we mmap'd */
00123   byte_t
00124    *data;
00125 
00126   /* used for partial buffer writes */
00127   int64_t
00128    partial_offset, partial_len;
00129 
00130 } aw_wwwhandler_xtn_getinfo_t;
00131 
00135 typedef struct aw_wwwhandler_xtn_s {
00136 
00137   aw_wwwhandler_xtn_type_cd_t
00138    type_cd;
00139 
00140   aw_wwwhandler_xtn_state_cd_t
00141    state_cd;
00142 
00143   /* http response code sent */
00144   int32_t
00145    response_cd;
00146 
00147   /* back ptr to monitor */
00148   aw_monitor_t
00149    *mon;
00150 
00151   /* back ptr to handler */
00152   struct aw_wwwhandler_s
00153    *h;
00154 
00155   /* socket we are talking over */
00156   int32_t
00157    txfd;
00158 
00159   /* the ip address of the requestor */
00160   aw_ip_addr_t
00161    clientip;
00162 
00163   /* ptr to last char of header */
00164   byte_t
00165    *eoheader;
00166 
00167   /* non-zero if want keep alive */
00168   int32_t
00169    keep_alive;
00170 
00171   /* count of re-use due to keep_alive */
00172   int32_t
00173    count;
00174 
00175   /* start time of xtn and timeout */
00176   aw_timestamp_t
00177    starttime,
00178    timeout;
00179 
00180   /* event id for timeout callback */
00181   aw_address_t
00182    timeout_eventid;
00183 
00184   /* pid of forked process (used w/cgi) */
00185   int32_t
00186    forkpid;
00187 
00188   /* if not NULL , will be appended to log file.
00189      (note: this should be malloc()'d as it will
00190      be free()'d in free_xtn() */
00191   byte_t
00192    *appendlog;
00193 
00194   /* bytes read or written (depending on state_cd) */
00195   u_int32_t
00196    nbytes;
00197 
00198   /* space for docroot/path or command */
00199   byte_t
00200    path[1024]; 
00201 
00202   /* space to read packets and use for scratch NOTE: needs to big enough to hold an entire HTTP header! */
00203   byte_t
00204    buffer[2048]; 
00205 
00206   /* state info for GET file command */
00207   aw_wwwhandler_xtn_getinfo_t
00208    getinfo;
00209 
00210   /* optional cgi command info */
00211   struct {
00212 
00213     /* copy of full header of request (0 terminated) */ 
00214     byte_t
00215     *header_block; 
00216 
00217     /* copy of full message body (0 terminated) for POST */
00218     byte_t
00219     *message_block;
00220 
00221     /* size of message in a POST */
00222     u_int32_t
00223     content_length;
00224 
00225     /* points to parsed strings in header_block for a GET */
00226     byte_t
00227     *command, *args, *headers;
00228     
00229     /* points to the end of the memory segment with the cgi args */
00230     byte_t
00231     *args_end;
00232 
00233     /* dictionary pointing to form args */
00234     aw_hashString_t
00235     *args_dict;
00236 
00237     /* writes the response header */
00238     aw_wwwhandler_io_code_t (*header)(aw_handler_args_t *args, struct aw_wwwhandler_xtn_s *xtn);
00239 
00240     /* does the work */
00241     aw_wwwhandler_io_code_t (*handler)(aw_handler_args_t *args, struct aw_wwwhandler_xtn_s *xtn);
00242 
00243     struct { /* created in aw_wwwhandler_append_cgipage(),
00244                 used in aw_wwwhandler_tx_cgipage(), 
00245                 and free'd in clear_cgi() */
00246 
00247       u_int32_t
00248         nbytes_allocated, /* how much we realloc'd */
00249         nbytes;           /* actual size of copied data */
00250 
00251       byte_t
00252       *data;
00253 
00254     } page ;
00255 
00256   } cgi;
00257 
00258   /* reply handler */
00259   aw_wwwhandler_io_code_t (*do_reply)(aw_handler_args_t *args, struct aw_wwwhandler_xtn_s *xtn);
00260 
00261 } aw_wwwhandler_xtn_t;
00262 
00263 
00267 typedef struct {
00268 
00269   /* socket accept()ing connections */
00270   int32_t
00271    sfd;
00272 
00273   aw_cache_allocator_t
00274    xtn_factory;
00275 
00276   /* active transactions in 2 luts */
00277   aw_hash32_t
00278    *fd2xtn,           
00279    *eventid2xtn;      
00281 } aw_wwwhandler_state_t;
00282 
00286 typedef struct {
00287 
00288   aw_event_sig_t
00289    xtn_info;                 
00291 } aw_wwwhandler_xtn_events_t;
00292 
00296 typedef struct aw_wwwhandler_s {
00297 
00298   aw_handler_t 
00299    handler_header;
00300 
00301   aw_wwwhandler_state_t
00302    state;
00303 
00304   u_int32_t 
00305    port;
00306 
00307   /* bind to specific ip if not 0 */
00308   aw_ip_addr_t 
00309    bind; 
00310 
00311   /* path of root of doc tree */
00312   byte_t
00313    *docroot;
00314 
00315   /* events to send on each xtn */
00316   aw_wwwhandler_xtn_events_t
00317    events;
00318 
00319   /* boolean configuration options */
00320   aw_wwwhandler_flags_t
00321    flags;
00322 
00323   /* value (seconds) to put in GET header Cache-Control: max-age=, if <=0 the no-cache is used */
00324   int32_t 
00325    get_cache_control_max_age; 
00326 
00327   /* if non-zero, then this is the max concurrent connections */
00328   u_int32_t
00329    max_connections;
00330 
00331   /* the default charset for Content-Type: header, defaults to ISO-8859-1 */
00332   byte_t
00333    default_charset[16];
00334 
00335   /* lookup that maps file suffix -> mime_type */
00336   aw_trie_t 
00337    *suffix2mime;
00338 
00339   int32_t (*dispatch_cgi)(aw_wwwhandler_xtn_t *xtn);           
00341   aw_handler_code_t (*start)(aw_monitor_t *mon, struct aw_wwwhandler_s *);  
00343   aw_handler_code_t (*stop)(aw_monitor_t *mon, struct aw_wwwhandler_s *);   
00345 } aw_wwwhandler_t ;
00346 
00347 
00348 /* mime types table rows */
00349 typedef struct {
00350 
00351   byte_t *suffix;
00352 
00353   u_int32_t suffixlen; /* strlen of suffix */
00354 
00355   byte_t *mime_type;
00356 
00357 } aw_suffix2mime_t;
00358 
00359 /* things you can pass down to constructor */
00360 typedef struct {
00361   
00362   byte_t *mime_types_path; 
00364   byte_t *default_charset; 
00366   aw_wwwhandler_flags_t flags;  
00368   aw_ip_addr_t bind; 
00370   int32_t get_cache_control_max_age; 
00372   u_int32_t max_connections;   
00374   aw_wwwhandler_xtn_events_t events; /*<! events to send */
00375 
00376   int32_t (*dispatch_cgi)(aw_wwwhandler_xtn_t *xtn);   
00378   aw_handler_code_t (*start)(aw_monitor_t *mon, struct aw_wwwhandler_s *);  
00380   aw_handler_code_t (*stop)(aw_monitor_t *mon, struct aw_wwwhandler_s *);   
00382 } aw_wwwhandler_options_t;
00383 
00394 aw_wwwhandler_io_code_t aw_wwwhandler_do_exec(aw_handler_args_t *args, aw_wwwhandler_xtn_t *xtn, 
00395                                               const byte_t *prog, const byte_t *prog_argv[],
00396                                               const byte_t *httpheader);
00397 
00405 aw_wwwhandler_io_code_t aw_wwwhandler_tx_file(aw_handler_args_t *args, aw_wwwhandler_xtn_t *xtn);
00406 
00414 aw_wwwhandler_io_code_t aw_wwwhandler_tx_header(aw_handler_args_t *args, aw_wwwhandler_xtn_t *xtn);
00415 
00423 inline const byte_t *aw_wwwhandler_find_cgiheader_line(aw_wwwhandler_xtn_t *xtn, byte_t *key);
00424 
00432 inline const byte_t *aw_wwwhandler_get_cgiarg(aw_wwwhandler_xtn_t *xtn, const byte_t *key);
00433 
00442 inline const byte_t *aw_wwwhandler_get_multi_cgiarg(aw_wwwhandler_xtn_t *xtn, const byte_t *key, const byte_t *last);
00443 
00451 int32_t aw_wwwhandler_printf_cgipage(aw_wwwhandler_xtn_t *xtn, const char *fmt,  ... ) __attribute__ ((format (printf, 2, 3)));
00452 
00461 int32_t aw_wwwhandler_append_cgipage(aw_wwwhandler_xtn_t *xtn, void *data, u_int32_t nbytes);
00462 
00463 
00472 aw_wwwhandler_io_code_t aw_wwwhandler_tx_cgi200(aw_handler_args_t *args, aw_wwwhandler_xtn_t *xtn, byte_t *headers);
00473 
00481 aw_wwwhandler_io_code_t aw_wwwhandler_tx_cgipage(aw_handler_args_t *args, aw_wwwhandler_xtn_t *xtn);
00482 
00496 aw_wwwhandler_io_code_t aw_wwwhandler_tx_200(aw_handler_args_t *args, aw_wwwhandler_xtn_t *xtn, 
00497                                              time_t lastmod, u_int64_t size, 
00498                                              int32_t max_age, 
00499                                              byte_t *mime_type, byte_t *charset,
00500                                              byte_t *headers);
00501 
00510 aw_wwwhandler_io_code_t aw_wwwhandler_tx_metadata(aw_handler_args_t *args, aw_wwwhandler_xtn_t *xtn, u_int32_t dir);
00511 
00521 aw_wwwhandler_io_code_t aw_wwwhandler_tx_error(aw_handler_args_t *args, aw_wwwhandler_xtn_t *xtn,
00522                                                 u_int32_t code, byte_t *message);
00523 
00533 int32_t aw_init_wwwhandler(aw_wwwhandler_t *h,
00534                            u_int32_t port,
00535                            byte_t *docroot,
00536                            aw_wwwhandler_options_t *options,
00537                            aw_logger_t *logger
00538                            );
00539 
00540 
00549 aw_wwwhandler_t *aw_create_wwwhandler( u_int32_t port,
00550                                        byte_t *docroot,
00551                                        aw_wwwhandler_options_t *options,
00552                                        aw_logger_t *logger
00553                                        );
00554 
00559 void aw_free_wwwhandler(aw_wwwhandler_t *p);
00560 
00561 /*\@}*/
00562 
00563 #endif
00564 
Aware 0.11.1 Copyright (C) 1998-2005 Russell Leighton (russ@elegant-software.com)