| 1 |
/* |
| 2 |
HTTP Request Handling |
| 3 |
Copyright (C) 1999-2006, 2008, Joe Orton <[email protected]> |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or |
| 6 |
modify it under the terms of the GNU Library General Public |
| 7 |
License as published by the Free Software Foundation; either |
| 8 |
version 2 of the License, or (at your option) any later version. |
| 9 |
|
| 10 |
This library is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
Library General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Library General Public |
| 16 |
License along with this library; if not, write to the Free |
| 17 |
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 18 |
MA 02111-1307, USA |
| 19 |
|
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef NE_REQUEST_H |
| 23 |
#define NE_REQUEST_H |
| 24 |
|
| 25 |
#include <time.h> |
| 26 |
|
| 27 |
#include "ne_utils.h" /* For ne_status */ |
| 28 |
#include "ne_string.h" /* For ne_buffer */ |
| 29 |
#include "ne_session.h" |
| 30 |
|
| 31 |
|
| 32 |
NE_BEGIN_DECLS |
| 33 |
|
| 34 |
#define NE_OK (0) /* Success */ |
| 35 |
#define NE_ERROR (1) /* Generic error; use ne_get_error(session) for message */ |
| 36 |
#define NE_LOOKUP (2) /* Server or proxy hostname lookup failed */ |
| 37 |
#define NE_AUTH (3) /* User authentication failed on server */ |
| 38 |
#define NE_PROXYAUTH (4) /* User authentication failed on proxy */ |
| 39 |
#define NE_CONNECT (5) /* Could not connect to server */ |
| 40 |
#define NE_TIMEOUT (6) /* Connection timed out */ |
| 41 |
#define NE_FAILED (7) /* The precondition failed */ |
| 42 |
#define NE_RETRY (8) /* Retry request (ne_end_request ONLY) */ |
| 43 |
#define NE_REDIRECT (9) /* See ne_redirect.h */ |
| 44 |
|
| 45 |
/* Opaque object representing a single HTTP request. */ |
| 46 |
typedef struct ne_request_s ne_request; |
| 47 |
|
| 48 |
/***** Request Handling *****/ |
| 49 |
|
| 50 |
/* Create a request in session 'sess', with given method and path. |
| 51 |
* 'path' must conform to the 'abs_path' grammar in RFC2396, with an |
| 52 |
* optional "? query" part, and MUST be URI-escaped by the caller. */ |
| 53 |
ne_request *ne_request_create(ne_session *sess, |
| 54 |
const char *method, const char *path); |
| 55 |
|
| 56 |
/* The request body will be taken from 'size' bytes of 'buffer'. */ |
| 57 |
void ne_set_request_body_buffer(ne_request *req, const char *buffer, |
| 58 |
size_t size); |
| 59 |
|
| 60 |
/* The request body will be taken from 'length' bytes read from the |
| 61 |
* file descriptor 'fd', starting from file offset 'offset'. */ |
| 62 |
void ne_set_request_body_fd(ne_request *req, int fd, |
| 63 |
ne_off_t offset, ne_off_t length); |
| 64 |
|
| 65 |
/* "Pull"-based request body provider: a callback which is invoked to |
| 66 |
* provide blocks of request body on demand. |
| 67 |
* |
| 68 |
* Before each time the body is provided, the callback will be called |
| 69 |
* once with buflen == 0. The body may have to be provided >1 time |
| 70 |
* per request (for authentication retries etc.). |
| 71 |
* |
| 72 |
* For a call with buflen == 0, the callback must return zero on success |
| 73 |
* or non-zero on error; the session error string must be set on error. |
| 74 |
* For a call with buflen > 0, the callback must return: |
| 75 |
* <0 : error, abort request; session error string must be set. |
| 76 |
* 0 : ignore 'buffer' contents, end of body. |
| 77 |
* 0 < x <= buflen : buffer contains x bytes of body data. */ |
| 78 |
typedef ssize_t (*ne_provide_body)(void *userdata, |
| 79 |
char *buffer, size_t buflen); |
| 80 |
|
| 81 |
/* Install a callback which is invoked as needed to provide the |
| 82 |
* request body, a block at a time. The total size of the request |
| 83 |
* body is 'length'; the callback must ensure that it returns no more |
| 84 |
* than 'length' bytes in total. */ |
| 85 |
void ne_set_request_body_provider(ne_request *req, ne_off_t length, |
| 86 |
ne_provide_body provider, void *userdata); |
| 87 |
|
| 88 |
|
| 89 |
/* Handling response bodies; two callbacks must be provided: |
| 90 |
* |
| 91 |
* 1) 'acceptance' callback: determines whether you want to handle the |
| 92 |
* response body given the response-status information, e.g., if you |
| 93 |
* only want 2xx responses, say so here. |
| 94 |
* |
| 95 |
* 2) 'reader' callback: passed blocks of the response-body as they |
| 96 |
* arrive, if the acceptance callback returned non-zero. */ |
| 97 |
|
| 98 |
/* 'acceptance' callback type. Return non-zero to accept the response, |
| 99 |
* else zero to ignore it. */ |
| 100 |
typedef int (*ne_accept_response)(void *userdata, ne_request *req, |
| 101 |
const ne_status *st); |
| 102 |
|
| 103 |
/* An 'acceptance' callback which only accepts 2xx-class responses. |
| 104 |
* Ignores userdata. */ |
| 105 |
int ne_accept_2xx(void *userdata, ne_request *req, const ne_status *st); |
| 106 |
|
| 107 |
/* An acceptance callback which accepts all responses. Ignores |
| 108 |
* userdata. */ |
| 109 |
int ne_accept_always(void *userdata, ne_request *req, const ne_status *st); |
| 110 |
|
| 111 |
/* Callback for reading a block of data. Returns zero on success, or |
| 112 |
* non-zero on error. If returning an error, the response will be |
| 113 |
* aborted and the callback will not be invoked again. The request |
| 114 |
* dispatch (or ne_read_response_block call) will fail with NE_ERROR; |
| 115 |
* the session error string should have been set by the callback. */ |
| 116 |
typedef int (*ne_block_reader)(void *userdata, const char *buf, size_t len); |
| 117 |
|
| 118 |
/* Add a response reader for the given request, with the given |
| 119 |
* acceptance function. userdata is passed as the first argument to |
| 120 |
* the acceptance + reader callbacks. |
| 121 |
* |
| 122 |
* The acceptance callback is called once each time the request is |
| 123 |
* sent: it may be sent >1 time because of authentication retries etc. |
| 124 |
* For each time the acceptance callback is called, if it returns |
| 125 |
* non-zero, blocks of the response body will be passed to the reader |
| 126 |
* callback as the response is read. After all the response body has |
| 127 |
* been read, the callback will be called with a 'len' argument of |
| 128 |
* zero. */ |
| 129 |
void ne_add_response_body_reader(ne_request *req, ne_accept_response accpt, |
| 130 |
ne_block_reader reader, void *userdata); |
| 131 |
|
| 132 |
/* Retrieve the value of the response header field with given name; |
| 133 |
* returns NULL if no response header with given name was found. The |
| 134 |
* return value is valid only until the next call to either |
| 135 |
* ne_request_destroy or ne_begin_request for this request. */ |
| 136 |
const char *ne_get_response_header(ne_request *req, const char *name); |
| 137 |
|
| 138 |
/* Iterator interface for response headers: if passed a NULL cursor, |
| 139 |
* returns the first header; if passed a non-NULL cursor pointer, |
| 140 |
* returns the next header. The return value is a cursor pointer: if |
| 141 |
* it is non-NULL, *name and *value are set to the name and value of |
| 142 |
* the header field. If the return value is NULL, no more headers are |
| 143 |
* found, *name and *value are undefined. |
| 144 |
* |
| 145 |
* The order in which response headers is returned is undefined. Both |
| 146 |
* the cursor and name/value pointers are valid only until the next |
| 147 |
* call to either ne_request_destroy or ne_begin_request for this |
| 148 |
* request. */ |
| 149 |
void *ne_response_header_iterate(ne_request *req, void *cursor, |
| 150 |
const char **name, const char **value); |
| 151 |
|
| 152 |
/* Adds a header to the request with given name and value. */ |
| 153 |
void ne_add_request_header(ne_request *req, const char *name, |
| 154 |
const char *value); |
| 155 |
/* Adds a header to the request with given name, using printf-like |
| 156 |
* format arguments for the value. */ |
| 157 |
void ne_print_request_header(ne_request *req, const char *name, |
| 158 |
const char *format, ...) |
| 159 |
ne_attribute((format(printf, 3, 4))); |
| 160 |
|
| 161 |
/* ne_request_dispatch: Sends the given request, and reads the |
| 162 |
* response. Returns: |
| 163 |
* - NE_OK if the request was sent and response read successfully |
| 164 |
* - NE_AUTH, NE_PROXYAUTH for a server or proxy server authentication error |
| 165 |
* - NE_CONNECT if connection could not be established |
| 166 |
* - NE_TIMEOUT if an timeout occurred sending or reading from the server |
| 167 |
* - NE_ERROR for other fatal dispatch errors |
| 168 |
* On any error, the session error string is set. On success or |
| 169 |
* authentication error, the actual response-status can be retrieved using |
| 170 |
* ne_get_status(). */ |
| 171 |
int ne_request_dispatch(ne_request *req); |
| 172 |
|
| 173 |
/* Returns a pointer to the response status information for the given |
| 174 |
* request; pointer is valid until request object is destroyed. */ |
| 175 |
const ne_status *ne_get_status(const ne_request *req) ne_attribute((const)); |
| 176 |
|
| 177 |
/* Returns pointer to session associated with request. */ |
| 178 |
ne_session *ne_get_session(const ne_request *req) ne_attribute((const)); |
| 179 |
|
| 180 |
/* Destroy memory associated with request pointer */ |
| 181 |
void ne_request_destroy(ne_request *req); |
| 182 |
|
| 183 |
/* "Caller-pulls" request interface. This is an ALTERNATIVE interface |
| 184 |
* to ne_request_dispatch: either use that, or do all this yourself: |
| 185 |
* |
| 186 |
* caller must call: |
| 187 |
* 1. ne_begin_request (fail if returns non-NE_OK) |
| 188 |
* 2. while(ne_read_response_block(...) > 0) ... loop ...; |
| 189 |
* (fail if ne_read_response_block returns <0) |
| 190 |
* 3. ne_end_request |
| 191 |
* |
| 192 |
* ne_end_request and ne_begin_request both return an NE_* code; if |
| 193 |
* ne_end_request returns NE_RETRY, you must restart the loop from (1) |
| 194 |
* above. */ |
| 195 |
int ne_begin_request(ne_request *req); |
| 196 |
int ne_end_request(ne_request *req); |
| 197 |
|
| 198 |
|
| 199 |
/* |
| 200 |
* |
| 201 |
* Abort open connexion in unstable state |
| 202 |
* |
| 203 |
* */ |
| 204 |
int ne_abort_request(ne_request* req); |
| 205 |
|
| 206 |
/* Read a block of the response into the passed buffer of size 'buflen'. |
| 207 |
* |
| 208 |
* Returns: |
| 209 |
* <0 - error, stop reading. |
| 210 |
* 0 - end of response |
| 211 |
* >0 - number of bytes read into buffer. |
| 212 |
*/ |
| 213 |
ssize_t ne_read_response_block(ne_request *req, char *buffer, size_t buflen); |
| 214 |
|
| 215 |
/* Read response blocks until end of response; exactly equivalent to |
| 216 |
* calling ne_read_response_block() until it returns 0. Returns |
| 217 |
* non-zero on error. */ |
| 218 |
int ne_discard_response(ne_request *req); |
| 219 |
|
| 220 |
/* Read response blocks until end of response, writing content to the |
| 221 |
* given file descriptor. Returns NE_ERROR on error. */ |
| 222 |
int ne_read_response_to_fd(ne_request *req, int fd); |
| 223 |
|
| 224 |
/* Defined request flags: */ |
| 225 |
typedef enum ne_request_flag_e { |
| 226 |
NE_REQFLAG_EXPECT100 = 0, /* enable this flag to enable use of the |
| 227 |
* "Expect: 100-continue" for the |
| 228 |
* request. */ |
| 229 |
|
| 230 |
NE_REQFLAG_IDEMPOTENT, /* disable this flag if the request uses a |
| 231 |
* non-idempotent method such as POST. */ |
| 232 |
|
| 233 |
NE_REQFLAG_LAST /* enum sentinel value */ |
| 234 |
} ne_request_flag; |
| 235 |
|
| 236 |
/* Set a new value for a particular request flag. */ |
| 237 |
void ne_set_request_flag(ne_request *req, ne_request_flag flag, int value); |
| 238 |
|
| 239 |
/* Return 0 if the given flag is not set, >0 it is set, or -1 if the |
| 240 |
* flag is not supported. */ |
| 241 |
int ne_get_request_flag(ne_request *req, ne_request_flag flag); |
| 242 |
|
| 243 |
|
| 244 |
/**** Request hooks handling *****/ |
| 245 |
|
| 246 |
typedef void (*ne_free_hooks)(void *cookie); |
| 247 |
|
| 248 |
/* Hook called when a request is created; passed the request method, |
| 249 |
* and the string used as the Request-URI (note that this may be a |
| 250 |
* absolute URI if a proxy is in use, an absolute path, a "*", etc). |
| 251 |
* A create_request hook is called exactly once per request. */ |
| 252 |
typedef void (*ne_create_request_fn)(ne_request *req, void *userdata, |
| 253 |
const char *method, const char *requri); |
| 254 |
void ne_hook_create_request(ne_session *sess, |
| 255 |
ne_create_request_fn fn, void *userdata); |
| 256 |
|
| 257 |
/* Hook called before the request is sent. 'header' is the raw HTTP |
| 258 |
* header before the trailing CRLF is added; more headers can be added |
| 259 |
* here. A pre_send hook may be called >1 time per request if the |
| 260 |
* request is retried due to a post_send hook returning NE_RETRY. */ |
| 261 |
typedef void (*ne_pre_send_fn)(ne_request *req, void *userdata, |
| 262 |
ne_buffer *header); |
| 263 |
void ne_hook_pre_send(ne_session *sess, ne_pre_send_fn fn, void *userdata); |
| 264 |
|
| 265 |
/* Hook called directly after the response headers have been read, but |
| 266 |
* before the resposnse body has been read. 'status' is the response |
| 267 |
* status-code. A post_header hook may be called >1 time per request |
| 268 |
* if the request is retried due to a post_send hook returning |
| 269 |
* NE_RETRY. */ |
| 270 |
typedef void (*ne_post_headers_fn)(ne_request *req, void *userdata, |
| 271 |
const ne_status *status); |
| 272 |
void ne_hook_post_headers(ne_session *sess, |
| 273 |
ne_post_headers_fn fn, void *userdata); |
| 274 |
|
| 275 |
/* Hook called after the request is dispatched (request sent, and |
| 276 |
* the entire response read). If an error occurred reading the response, |
| 277 |
* this hook will not run. May return: |
| 278 |
* NE_OK everything is okay |
| 279 |
* NE_RETRY try sending the request again. |
| 280 |
* anything else signifies an error, and the request is failed. The return |
| 281 |
* code is passed back the _dispatch caller, so the session error must |
| 282 |
* also be set appropriately (ne_set_error). |
| 283 |
*/ |
| 284 |
typedef int (*ne_post_send_fn)(ne_request *req, void *userdata, |
| 285 |
const ne_status *status); |
| 286 |
void ne_hook_post_send(ne_session *sess, ne_post_send_fn fn, void *userdata); |
| 287 |
|
| 288 |
/* Hook called when the function is destroyed. */ |
| 289 |
typedef void (*ne_destroy_req_fn)(ne_request *req, void *userdata); |
| 290 |
void ne_hook_destroy_request(ne_session *sess, |
| 291 |
ne_destroy_req_fn fn, void *userdata); |
| 292 |
|
| 293 |
typedef void (*ne_destroy_sess_fn)(void *userdata); |
| 294 |
/* Hook called when the session is about to be destroyed. */ |
| 295 |
void ne_hook_destroy_session(ne_session *sess, |
| 296 |
ne_destroy_sess_fn fn, void *userdata); |
| 297 |
|
| 298 |
typedef void (*ne_close_conn_fn)(void *userdata); |
| 299 |
/* Hook called when the connection is closed; note that this hook |
| 300 |
* may be called *AFTER* the destroy_session hook. */ |
| 301 |
void ne_hook_close_conn(ne_session *sess, ne_close_conn_fn fn, void *userdata); |
| 302 |
|
| 303 |
/* The ne_unhook_* functions remove a hook registered with the given |
| 304 |
* session. If a hook is found which was registered with a given |
| 305 |
* function 'fn', and userdata pointer 'userdata', then it will be |
| 306 |
* removed from the hooks list. |
| 307 |
* |
| 308 |
* It is unsafe to use any of these functions from a hook function to |
| 309 |
* unregister itself, except for ne_unhook_destroy_request. */ |
| 310 |
void ne_unhook_create_request(ne_session *sess, |
| 311 |
ne_create_request_fn fn, void *userdata); |
| 312 |
void ne_unhook_pre_send(ne_session *sess, ne_pre_send_fn fn, void *userdata); |
| 313 |
void ne_unhook_post_headers(ne_session *sess, ne_post_headers_fn fn, void *userdata); |
| 314 |
void ne_unhook_post_send(ne_session *sess, ne_post_send_fn fn, void *userdata); |
| 315 |
void ne_unhook_destroy_request(ne_session *sess, |
| 316 |
ne_destroy_req_fn fn, void *userdata); |
| 317 |
void ne_unhook_destroy_session(ne_session *sess, |
| 318 |
ne_destroy_sess_fn fn, void *userdata); |
| 319 |
void ne_unhook_close_conn(ne_session *sess, |
| 320 |
ne_close_conn_fn fn, void *userdata); |
| 321 |
|
| 322 |
/* Store an opaque context for the request, 'priv' is returned by a |
| 323 |
* call to ne_request_get_private with the same ID. */ |
| 324 |
void ne_set_request_private(ne_request *req, const char *id, void *priv); |
| 325 |
void *ne_get_request_private(ne_request *req, const char *id); |
| 326 |
|
| 327 |
NE_END_DECLS |
| 328 |
|
| 329 |
#endif /* NE_REQUEST_H */ |