1 #ifndef __GSK_HTTP_CONTENT_H_ 2 #define __GSK_HTTP_CONTENT_H_ 3 4 #include "gskhttpserver.h" 5 #include "../mime/gskmimemultipartpiece.h" 6 #include "../gsksocketaddress.h" 7 8 G_BEGIN_DECLS 9 10 /* TODO: 11 - provide error page handler functions (eg something 12 which takes a server,request,response_status,error_message 13 and responds to the user with it. one variant 14 registered with a particular error code, 15 one that handles any error code. 16 (provide suitable defaults) 17 18 - likewise, redirect handlers. 19 20 - ... 21 22 - make a GskTableTree type for managing hierarchical data 23 like this, to clean it up (pretty hard!) 24 */ 25 26 /* class to help run an http server */ 27 28 /* opaque class */ 29 typedef struct _GskHttpContentHandler GskHttpContentHandler; 30 typedef struct _GskHttpContent GskHttpContent; 31 typedef struct _GskHttpContentId GskHttpContentId; 32 33 typedef enum 34 { 35 GSK_HTTP_CONTENT_OK, 36 GSK_HTTP_CONTENT_CHAIN, 37 GSK_HTTP_CONTENT_ERROR /* causes a 500 error to result */ 38 } GskHttpContentResult; 39 40 typedef GskHttpContentResult (*GskHttpContentFunc) (GskHttpContent *content, 41 GskHttpContentHandler *handler, 42 GskHttpServer *server, 43 GskHttpRequest *request, 44 GskStream *post_data, 45 gpointer data); 46 47 48 /* note: CGIs cannot chain */ 49 typedef void (*GskHttpContentCGIFunc) (GskHttpContent *content, 50 GskHttpContentHandler *handler, 51 GskHttpServer *server, 52 GskHttpRequest *request, 53 guint n_vars, 54 GskMimeMultipartPiece **vars, 55 gpointer data); 56 57 58 struct _GskHttpContentId 59 { 60 const char *host; 61 const char *user_agent_prefix; 62 const char *path; 63 const char *path_prefix; 64 const char *path_suffix; 65 }; 66 #define GSK_HTTP_CONTENT_ID_INIT {NULL,NULL,NULL,NULL,NULL} 67 68 /* the order hook tables are tried is: 69 with user_agent, with path and vhost 70 with user_agent, with vhost 71 with user_agent, with path 72 with user_agent, no path or host 73 without user_agent, with path and vhost 74 without user_agent, with vhost 75 without user_agent, with path 76 without user_agent, no path or host */ 77 78 GskHttpContent *gsk_http_content_new (void); 79 GskHttpContentHandler * 80 gsk_http_content_handler_new (GskHttpContentFunc func, 81 gpointer data, 82 GDestroyNotify destroy); 83 84 GskHttpContentHandler * 85 gsk_http_content_handler_new_cgi (GskHttpContentCGIFunc func, 86 gpointer data, 87 GDestroyNotify destroy); 88 89 void gsk_http_content_handler_ref (GskHttpContentHandler *handler); 90 void gsk_http_content_handler_unref(GskHttpContentHandler *handler); 91 92 93 typedef enum 94 { 95 GSK_HTTP_CONTENT_PREPEND, 96 GSK_HTTP_CONTENT_APPEND, 97 GSK_HTTP_CONTENT_REPLACE 98 } GskHttpContentAction; 99 100 void gsk_http_content_add_handler (GskHttpContent *content, 101 const GskHttpContentId *id, 102 GskHttpContentHandler *handler, 103 GskHttpContentAction action); 104 105 void gsk_http_content_set_mime_type(GskHttpContent *content, 106 const char *prefix, 107 const char *suffix, 108 const char *type, 109 const char *subtype); 110 void gsk_http_content_set_default_mime_type 111 (GskHttpContent *content, 112 const char *type, 113 const char *subtype); 114 gboolean gsk_http_content_get_mime_type(GskHttpContent *content, 115 const char *path, 116 const char **type_out, 117 const char **subtype_out); 118 void gsk_http_content_set_idle_timeout (GskHttpContent *content); 119 120 typedef void (*GskHttpContentErrorHandler)(GskHttpContent *content, 121 GError *error, 122 GskHttpServer *server, 123 GskHttpRequest *request, 124 GskHttpStatus code, 125 gpointer data); 126 127 void gsk_http_content_set_error_handler (GskHttpContent *content, 128 GskHttpContentErrorHandler handler, 129 gpointer data, 130 GDestroyNotify destroy); 131 132 /* helpers */ 133 void gsk_http_content_add_data (GskHttpContent *content, 134 const GskHttpContentId *id, 135 gconstpointer data, 136 guint data_len, 137 gpointer destroy_data, 138 GDestroyNotify destroy); 139 void gsk_http_content_add_data_by_path 140 (GskHttpContent *content, 141 const char *path, 142 gconstpointer data, 143 guint data_len, 144 gpointer destroy_data, 145 GDestroyNotify destroy); 146 typedef enum 147 { 148 GSK_HTTP_CONTENT_FILE_EXACT, 149 GSK_HTTP_CONTENT_FILE_DIR, 150 GSK_HTTP_CONTENT_FILE_DIR_TREE 151 } GskHttpContentFileType; 152 void gsk_http_content_add_file (GskHttpContent *content, 153 const char *path, 154 const char *fs_path, 155 GskHttpContentFileType type); 156 157 /* note: id must include a path_prefix */ 158 void gsk_http_content_add_file_by_id 159 (GskHttpContent *content, 160 const GskHttpContentId *id, 161 const char *fs_path, 162 GskHttpContentFileType type); 163 164 165 /* --- serving pages --- */ 166 gboolean gsk_http_content_listen (GskHttpContent *content, 167 GskSocketAddress *address, 168 GError **error); 169 void gsk_http_content_respond (GskHttpContent *content, 170 GskHttpServer *server, 171 GskHttpRequest *request, 172 GskStream *post_data); 173 void gsk_http_content_manage_server (GskHttpContent *content, 174 GskHttpServer *server); 175 176 G_END_DECLS 177 178 #endif 179