00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00036 #ifndef _core_http_h_
00037 #define _core_http_h_
00038
00039 #include "common.h"
00040
00041 #include "if/core/http.h"
00042 #include "lib/host_addr.h"
00043
00044 #define HTTP_PORT 80
00046 typedef enum {
00047 HTTP_CONTENT_ENCODING_IDENTITY,
00048 HTTP_CONTENT_ENCODING_DEFLATE,
00049 HTTP_CONTENT_ENCODING_GZIP
00050 } http_content_encoding_t;
00051
00056 typedef enum {
00057 HTTP_EXTRA_LINE,
00058 HTTP_EXTRA_CALLBACK,
00059 HTTP_EXTRA_BODY
00060 } http_extra_type_t;
00061
00073 typedef size_t (*http_status_cb_t)(
00074 gchar *buf, size_t size, gpointer arg, guint32 flags);
00075
00076 typedef struct {
00077 http_extra_type_t he_type;
00078 union {
00079 const gchar *u_msg;
00080 struct {
00081 http_status_cb_t u_cb;
00082 gpointer u_arg;
00083 } u_cbk;
00084 } u;
00085 } http_extra_desc_t;
00086
00087 #define he_msg u.u_msg
00088 #define he_cb u.u_cbk.u_cb
00089 #define he_arg u.u_cbk.u_arg
00090
00091 static inline void
00092 http_extra_callback_set(http_extra_desc_t *he,
00093 http_status_cb_t callback, gpointer user_arg)
00094 {
00095 he->he_type = HTTP_EXTRA_CALLBACK;
00096 he->he_cb = callback;
00097 he->he_arg = user_arg;
00098 }
00099
00100 static inline void
00101 http_extra_line_set(http_extra_desc_t *he, const gchar *msg)
00102 {
00103 he->he_type = HTTP_EXTRA_LINE;
00104 he->he_msg = msg;
00105 }
00106
00107 static inline void
00108 http_extra_body_set(http_extra_desc_t *he, const gchar *body)
00109 {
00110 he->he_type = HTTP_EXTRA_BODY;
00111 he->he_msg = body;
00112 }
00113
00114
00115
00116
00117
00118 #define HTTP_CBF_SMALL_REPLY 0x00000001
00119 #define HTTP_CBF_BW_SATURATED 0x00000002
00120 #define HTTP_CBF_BUSY_SIGNAL 0x00000004
00121 #define HTTP_CBF_SHOW_RANGES 0x00000008
00123 struct header;
00124 struct http_async;
00125
00130 typedef gboolean (*http_header_cb_t)(
00131 struct http_async *, struct header *, gint code, const gchar *message);
00132
00136 typedef void (*http_data_cb_t)(struct http_async *, gchar *data, gint len);
00137
00138 typedef enum {
00139 HTTP_ASYNC_SYSERR,
00140 HTTP_ASYNC_ERROR,
00141 HTTP_ASYNC_HEADER,
00142 HTTP_ASYNC_HTTP
00143 } http_errtype_t;
00144
00145 typedef struct {
00146 struct header *header;
00147 const gchar *message;
00148 gint code;
00149 } http_error_t;
00150
00156 typedef void (*http_error_cb_t)(
00157 struct http_async *, http_errtype_t error, gpointer val);
00158
00163 typedef void (*http_user_free_t)(gpointer data);
00164
00169 typedef size_t (*http_op_request_t)(struct http_async *, gchar *buf, size_t len,
00170 const gchar *verb, const gchar *path, const gchar *host, guint16 port);
00171
00172
00173
00174
00175
00176 #define HTTP_ASYNC_OK 0
00177 #define HTTP_ASYNC_BAD_URL 1
00178 #define HTTP_ASYNC_CONN_FAILED 2
00179 #define HTTP_ASYNC_IO_ERROR 3
00180 #define HTTP_ASYNC_REQ2BIG 4
00181 #define HTTP_ASYNC_HEAD2BIG 5
00182 #define HTTP_ASYNC_CANCELLED 6
00183 #define HTTP_ASYNC_EOF 7
00184 #define HTTP_ASYNC_BAD_STATUS 8
00185 #define HTTP_ASYNC_NO_LOCATION 9
00186 #define HTTP_ASYNC_CONN_TIMEOUT 10
00187 #define HTTP_ASYNC_TIMEOUT 11
00188 #define HTTP_ASYNC_NESTED 12
00189 #define HTTP_ASYNC_BAD_LOCATION_URI 13
00190 #define HTTP_ASYNC_CLOSED 14
00191 #define HTTP_ASYNC_REDIRECTED 15
00193 extern guint http_async_errno;
00194
00199 typedef enum {
00200 HTTP_URL_OK = 0,
00201 HTTP_URL_NOT_HTTP,
00202 HTTP_URL_MULTIPLE_CREDENTIALS,
00203 HTTP_URL_BAD_CREDENTIALS,
00204 HTTP_URL_BAD_PORT_PARSING,
00205 HTTP_URL_BAD_PORT_RANGE,
00206 HTTP_URL_BAD_HOST_PART,
00207 HTTP_URL_HOSTNAME_UNKNOWN,
00208 HTTP_URL_MISSING_URI
00209 } http_url_error_t;
00210
00211 extern http_url_error_t http_url_errno;
00212
00217 typedef void (*http_state_change_t)(struct http_async *, http_state_t newstate);
00218
00223 typedef struct http_buffer {
00224 gchar *hb_arena;
00225 gchar *hb_rptr;
00226 gchar *hb_end;
00227 gint hb_len;
00228 } http_buffer_t;
00229
00230 #define http_buffer_base(hb) ((hb)->hb_arena)
00231 #define http_buffer_length(hb) ((hb)->hb_len)
00232 #define http_buffer_read_base(hb) ((hb)->hb_rptr)
00233 #define http_buffer_unread(hb) ((hb)->hb_end - (hb)->hb_rptr)
00234
00235 #define http_buffer_add_read(hb,tx) do { (hb)->hb_rptr += (tx); } while (0)
00236
00237
00238
00239
00240
00241 struct gnutella_socket;
00242
00243 void http_timer(time_t now);
00244
00245 gboolean http_send_status(struct gnutella_socket *s,
00246 gint code, gboolean keep_alive, http_extra_desc_t *hev, gint hevcnt,
00247 const gchar *reason, ...) G_GNUC_PRINTF(6, 7);
00248
00249 size_t http_hostname_add(
00250 gchar *buf, size_t size, gpointer arg, guint32 flags);
00251 size_t http_retry_after_add(
00252 gchar *buf, size_t size, gpointer arg, guint32 flags);
00253
00254 gint http_status_parse(const gchar *line,
00255 const gchar *proto, const gchar **msg, guint *major, guint *minor);
00256
00257 gboolean http_extract_version(
00258 const gchar *request, size_t len, guint *major, guint *minor);
00259
00260 http_buffer_t *http_buffer_alloc(gchar *buf, size_t len, size_t written);
00261 void http_buffer_free(http_buffer_t *b);
00262
00263 gint
00264 http_content_range_parse(const gchar *buf,
00265 filesize_t *start, filesize_t *end, filesize_t *total);
00266
00267 filesize_t http_range_size(const GSList *list);
00268 void http_range_free(GSList *list);
00269 GSList *http_range_parse(const gchar *field, const gchar *value,
00270 filesize_t size, const gchar *vendor);
00271 gboolean http_range_contains(GSList *ranges, filesize_t from, filesize_t to);
00272
00273 const gchar *http_url_strerror(http_url_error_t errnum);
00274 gboolean http_url_parse(
00275 const gchar *url, guint16 *port, const gchar **host, const gchar **path);
00276
00277 struct http_async *http_async_get(
00278 const gchar *url,
00279 http_header_cb_t header_ind,
00280 http_data_cb_t data_ind,
00281 http_error_cb_t error_ind);
00282
00283 struct http_async *http_async_get_addr(
00284 const gchar *path,
00285 const host_addr_t,
00286 guint16 port,
00287 http_header_cb_t header_ind,
00288 http_data_cb_t data_ind,
00289 http_error_cb_t error_ind);
00290
00291 const gchar *http_async_strerror(guint errnum);
00292 const gchar *http_async_info(
00293 struct http_async *handle, const gchar **req, const gchar **path,
00294 host_addr_t *addr, guint16 *port);
00295 void http_async_connected(struct http_async *handle);
00296 void http_async_close(struct http_async *handle);
00297 void http_async_cancel(struct http_async *handle);
00298 void http_async_error(struct http_async *handle, gint code);
00299 http_state_t http_async_state(struct http_async *handle);
00300
00301 void http_async_set_opaque(struct http_async *handle,
00302 gpointer data, http_user_free_t fn);
00303 gpointer http_async_get_opaque(struct http_async *handle);
00304 void http_async_log_error(struct http_async *handle,
00305 http_errtype_t type, gpointer v);
00306 void http_async_log_error_dbg(struct http_async *handle,
00307 http_errtype_t type, gpointer v, guint32 dbg_level);
00308
00309 void http_async_on_state_change(struct http_async *handle,
00310 http_state_change_t fn);
00311 void http_async_allow_redirects(struct http_async *handle, gboolean allow);
00312 void http_async_set_op_request(struct http_async *handle, http_op_request_t op);
00313
00314 void http_close(void);
00315
00316 #endif
00317
00318