1
/* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
3
* Licensed under the Apache License, Version 2.0 (the "License");
4
* you may not use this file except in compliance with the License.
5
* You may obtain a copy of the License at
7
* http://www.apache.org/licenses/LICENSE-2.0
9
* Unless required by applicable law or agreed to in writing, software
10
* distributed under the License is distributed on an "AS IS" BASIS,
11
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
* See the License for the specific language governing permissions and
13
* limitations under the License.
21
* @brief Main serf header file
25
#include <apr_errno.h>
26
#include <apr_allocator.h>
27
#include <apr_pools.h>
28
#include <apr_network_io.h>
37
/* Forward declare some structures */
38
typedef struct serf_context_t serf_context_t;
40
typedef struct serf_bucket_t serf_bucket_t;
41
typedef struct serf_bucket_type_t serf_bucket_type_t;
42
typedef struct serf_bucket_alloc_t serf_bucket_alloc_t;
44
typedef struct serf_connection_t serf_connection_t;
45
typedef struct serf_listener_t serf_listener_t;
46
typedef struct serf_incoming_t serf_incoming_t;
47
typedef struct serf_incoming_request_t serf_incoming_request_t;
49
typedef struct serf_request_t serf_request_t;
53
* @defgroup serf high-level constructs
59
* Serf-specific error codes
61
#define SERF_ERROR_RANGE 100
63
/* This code is for when this is the last response on this connection:
64
* i.e. do not send any more requests on this connection or expect
67
#define SERF_ERROR_CLOSING (APR_OS_START_USERERR + SERF_ERROR_RANGE + 1)
68
/* This code is for when the connection terminated before the request
69
* could be processed on the other side.
71
#define SERF_ERROR_REQUEST_LOST (APR_OS_START_USERERR + SERF_ERROR_RANGE + 2)
72
/* This code is for when the connection is blocked - we can not proceed
73
* until something happens - generally due to SSL negotiation-like behavior
74
* where a write() is blocked until a read() is processed.
76
#define SERF_ERROR_WAIT_CONN (APR_OS_START_USERERR + SERF_ERROR_RANGE + 3)
77
/* This code is for when something went wrong during deflating compressed
78
* data e.g. a CRC error. */
79
#define SERF_ERROR_DECOMPRESSION_FAILED (APR_OS_START_USERERR + \
81
/* This code is for when a response received from a http server is not in
82
* http-compliant syntax. */
83
#define SERF_ERROR_BAD_HTTP_RESPONSE (APR_OS_START_USERERR + \
86
/* General authentication related errors */
87
#define SERF_ERROR_AUTHN_FAILED (APR_OS_START_USERERR + SERF_ERROR_RANGE + 90)
89
/* None of the available authn mechanisms for the request are supported */
90
#define SERF_ERROR_AUTHN_NOT_SUPPORTED (APR_OS_START_USERERR + SERF_ERROR_RANGE + 91)
92
/* Authn was requested by the server but the header lacked some attribute */
93
#define SERF_ERROR_AUTHN_MISSING_ATTRIBUTE (APR_OS_START_USERERR + SERF_ERROR_RANGE + 92)
95
/* Authentication handler initialization related errors */
96
#define SERF_ERROR_AUTHN_INITALIZATION_FAILED (APR_OS_START_USERERR +\
97
SERF_ERROR_RANGE + 93)
99
/* This macro groups errors potentially raised when reading a http response. */
100
#define SERF_BAD_RESPONSE_ERROR(status) ((status) \
101
&& ((SERF_ERROR_DECOMPRESSION_FAILED == (status)) \
102
||(SERF_ERROR_BAD_HTTP_RESPONSE == (status))))
105
* Return a string that describes the specified error code.
107
* If the error code is not one of the above Serf error codes, then
108
* NULL will be returned.
110
* Note regarding lifetime: the string is a statically-allocated constant
112
const char *serf_error_string(apr_status_t errcode);
116
* Create a new context for serf operations.
118
* A serf context defines a control loop which processes multiple
119
* connections simultaneously.
121
* The context will be allocated within @a pool.
123
serf_context_t *serf_context_create(
127
* Callback function. Add a socket to the externally managed poll set.
129
* Both @a pfd and @a serf_baton should be used when calling serf_event_trigger
132
typedef apr_status_t (*serf_socket_add_t)(
138
* Callback function. Remove the socket, identified by both @a pfd and
139
* @a serf_baton from the externally managed poll set.
141
typedef apr_status_t (*serf_socket_remove_t)(
146
/* Create a new context for serf operations.
148
* Use this function to make serf not use its internal control loop, but
149
* instead rely on an external event loop. Serf will use the @a addf and @a rmf
150
* callbacks to notify of any event on a connection. The @a user_baton will be
151
* passed through the addf and rmf callbacks.
153
* The context will be allocated within @a pool.
155
serf_context_t *serf_context_create_ex(
157
serf_socket_add_t addf,
158
serf_socket_remove_t rmf,
162
* Make serf process events on a connection, identified by both @a pfd and
165
* Any outbound data is delivered, and incoming data is made available to
166
* the associated response handlers and their buckets.
168
* If any data is processed (incoming or outgoing), then this function will
169
* return with APR_SUCCESS.
171
apr_status_t serf_event_trigger(
174
const apr_pollfd_t *pfd);
176
/** @see serf_context_run should not block at all. */
177
#define SERF_DURATION_NOBLOCK 0
178
/** @see serf_context_run should run for (nearly) "forever". */
179
#define SERF_DURATION_FOREVER 2000000000 /* approx 1^31 */
182
* Run the main networking control loop.
184
* The set of connections defined by the serf context @a ctx are processed.
185
* Any outbound data is delivered, and incoming data is made available to
186
* the associated response handlers and their buckets. This function will
187
* block on the network for no longer than @a duration microseconds.
189
* If any data is processed (incoming or outgoing), then this function will
190
* return with APR_SUCCESS. Typically, the caller will just want to call it
191
* again to continue processing data.
193
* If no activity occurs within the specified timeout duration, then
194
* APR_TIMEUP is returned.
196
* All temporary allocations will be made in @a pool.
198
apr_status_t serf_context_run(
200
apr_short_interval_time_t duration,
204
apr_status_t serf_context_prerun(
205
serf_context_t *ctx);
208
* Callback function for progress information. @a progress indicates cumulative
209
* number of bytes read or written, for the whole context.
211
typedef void (*serf_progress_t)(
212
void *progress_baton,
217
* Sets the progress callback function. @a progress_func will be called every
218
* time bytes are read of or written on a socket.
220
void serf_context_set_progress_cb(
222
const serf_progress_t progress_func,
223
void *progress_baton);
228
* @defgroup serf connections and requests
234
* When a connection is established, the application needs to wrap some
235
* buckets around @a skt to enable serf to process incoming responses. This
236
* is the control point for assembling connection-level processing logic
237
* around the given socket.
239
* The @a setup_baton is the baton established at connection creation time.
241
* This callback corresponds to reading from the server. Since this is an
242
* on-demand activity, we use a callback. The corresponding write operation
243
* is based on the @see serf_request_deliver function, where the application
244
* can assemble the appropriate bucket(s) before delivery.
246
* The returned bucket should live at least as long as the connection itself.
247
* It is assumed that an appropriate allocator is passed in @a setup_baton.
248
* ### we may want to create a connection-level allocator and pass that
249
* ### along. however, that allocator would *only* be used for this
250
* ### callback. it may be wasteful to create a per-conn allocator, so this
251
* ### baton-based, app-responsible form might be best.
253
* Responsibility for the buckets is passed to the serf library. They will be
254
* destroyed when the connection is closed.
256
* All temporary allocations should be made in @a pool.
258
typedef apr_status_t (*serf_connection_setup_t)(
260
serf_bucket_t **read_bkt,
261
serf_bucket_t **write_bkt,
266
* ### need to update docco w.r.t socket. became "stream" recently.
267
* ### the stream does not have a barrier, this callback should generally
268
* ### add a barrier around the stream before incorporating it into a
269
* ### response bucket stack.
270
* ### should serf add the barrier automatically to protect its data
271
* ### structure? i.e. the passed bucket becomes owned rather than
272
* ### borrowed. that might suit overall semantics better.
273
* Accept an incoming response for @a request, and its @a socket. A bucket
274
* for the response should be constructed and returned. This is the control
275
* point for assembling the appropriate wrapper buckets around the socket to
276
* enable processing of the incoming response.
278
* The @a acceptor_baton is the baton provided when the specified request
281
* The request's pool and bucket allocator should be used for any allocations
282
* that need to live for the duration of the response. Care should be taken
283
* to bound the amount of memory stored in this pool -- to ensure that
284
* allocations are not proportional to the amount of data in the response.
286
* Responsibility for the bucket is passed to the serf library. It will be
287
* destroyed when the response has been fully read (the bucket returns an
288
* APR_EOF status from its read functions).
290
* All temporary allocations should be made in @a pool.
292
/* ### do we need to return an error? */
293
typedef serf_bucket_t * (*serf_response_acceptor_t)(
294
serf_request_t *request,
295
serf_bucket_t *stream,
296
void *acceptor_baton,
300
* Notification callback for when a connection closes.
302
* This callback is used to inform an application that the @a conn
303
* connection has been (abnormally) closed. The @a closed_baton is the
304
* baton provided when the connection was first opened. The reason for
305
* closure is given in @a why, and will be APR_SUCCESS if the application
306
* requested closure (by clearing the pool used to allocate this
307
* connection or calling serf_connection_close).
309
* All temporary allocations should be made in @a pool.
311
typedef void (*serf_connection_closed_t)(
312
serf_connection_t *conn,
318
* Response data has arrived and should be processed.
320
* Whenever response data for @a request arrives (initially, or continued data
321
* arrival), this handler is invoked. The response data is available in the
322
* @a response bucket. The @a handler_baton is passed along from the baton
323
* provided by the request setup callback (@see serf_request_setup_t).
325
* The handler MUST process data from the @a response bucket until the
326
* bucket's read function states it would block (see APR_STATUS_IS_EAGAIN).
327
* The handler is invoked only when new data arrives. If no further data
328
* arrives, and the handler does not process all available data, then the
329
* system can result in a deadlock around the unprocessed, but read, data.
331
* The handler should return APR_EOF when the response has been fully read.
332
* If calling the handler again would block, APR_EAGAIN should be returned.
333
* If the handler should be invoked again, simply return APR_SUCCESS.
335
* Note: if the connection closed (at the request of the application, or
336
* because of an (abnormal) termination) while a request is being delivered,
337
* or before a response arrives, then @a response will be NULL. This is the
338
* signal that the request was not delivered properly, and no further
339
* response should be expected (this callback will not be invoked again).
340
* If a request is injected into the connection (during this callback's
341
* execution, or otherwise), then the connection will be reopened.
343
* All temporary allocations should be made in @a pool.
345
typedef apr_status_t (*serf_response_handler_t)(
346
serf_request_t *request,
347
serf_bucket_t *response,
352
* Callback function to be implemented by the application, so that serf
353
* can handle server and proxy authentication.
354
* code = 401 (server) or 407 (proxy).
355
* baton = the baton passed to serf_context_run.
356
* authn_type = one of "Basic", "Digest".
358
typedef apr_status_t (*serf_credentials_callback_t)(
361
serf_request_t *request, void *baton,
362
int code, const char *authn_type,
367
* Create a new connection associated with the @a ctx serf context.
369
* A connection will be created to (eventually) connect to the address
370
* specified by @a address. The address must live at least as long as
371
* @a pool (thus, as long as the connection object).
373
* The connection object will be allocated within @a pool. Clearing or
374
* destroying this pool will close the connection, and terminate any
375
* outstanding requests or responses.
377
* When the connection is closed (upon request or because of an error),
378
* then the @a closed callback is invoked, and @a closed_baton is passed.
380
* ### doc on setup(_baton). tweak below comment re: acceptor.
381
* NULL may be passed for @a acceptor and @a closed; default implementations
384
* Note: the connection is not made immediately. It will be opened on
385
* the next call to @see serf_context_run.
387
serf_connection_t *serf_connection_create(
389
apr_sockaddr_t *address,
390
serf_connection_setup_t setup,
392
serf_connection_closed_t closed,
397
* Create a new connection associated with the @a ctx serf context.
399
* A connection will be created to (eventually) connect to the address
400
* specified by @a address. The address must live at least as long as
401
* @a pool (thus, as long as the connection object).
403
* The host address will be looked up based on the hostname in @a host_info.
405
* The connection object will be allocated within @a pool. Clearing or
406
* destroying this pool will close the connection, and terminate any
407
* outstanding requests or responses.
409
* When the connection is closed (upon request or because of an error),
410
* then the @a closed callback is invoked, and @a closed_baton is passed.
412
* ### doc on setup(_baton). tweak below comment re: acceptor.
413
* NULL may be passed for @a acceptor and @a closed; default implementations
416
* Note: the connection is not made immediately. It will be opened on
417
* the next call to @see serf_context_run.
419
apr_status_t serf_connection_create2(
420
serf_connection_t **conn,
423
serf_connection_setup_t setup,
425
serf_connection_closed_t closed,
430
typedef apr_status_t (*serf_accept_client_t)(
434
apr_socket_t *insock,
437
apr_status_t serf_listener_create(
438
serf_listener_t **listener,
443
serf_accept_client_t accept_func,
446
typedef apr_status_t (*serf_incoming_request_cb_t)(
448
serf_incoming_request_t *req,
452
apr_status_t serf_incoming_create(
453
serf_incoming_t **client,
455
apr_socket_t *insock,
457
serf_incoming_request_cb_t request,
464
* Reset the connection, but re-open the socket again.
466
apr_status_t serf_connection_reset(
467
serf_connection_t *conn);
470
* Close the connection associated with @a conn and cancel all pending requests.
472
* The closed callback passed to serf_connection_create() will be invoked
475
apr_status_t serf_connection_close(
476
serf_connection_t *conn);
479
* Sets the maximum number of outstanding requests @a max_requests on the
480
* connection @a conn. Setting max_requests to 0 means unlimited (the default).
481
* Ex.: setting max_requests to 1 means a request is sent when a response on the
482
* previous request was received and handled.
484
void serf_connection_set_max_outstanding_requests(
485
serf_connection_t *conn,
486
unsigned int max_requests);
488
void serf_connection_set_async_responses(
489
serf_connection_t *conn,
490
serf_response_acceptor_t acceptor,
491
void *acceptor_baton,
492
serf_response_handler_t handler,
493
void *handler_baton);
496
* Setup the @a request for delivery on its connection.
498
* Right before this is invoked, @a pool will be built within the
499
* connection's pool for the request to use. The associated response will
500
* be allocated within that subpool. An associated bucket allocator will
501
* be built. These items may be fetched from the request object through
502
* @see serf_request_get_pool or @see serf_request_get_alloc.
504
* The content of the request is specified by the @a req_bkt bucket. When
505
* a response arrives, the @a acceptor callback will be invoked (along with
506
* the @a acceptor_baton) to produce a response bucket. That bucket will then
507
* be passed to @a handler, along with the @a handler_baton.
509
* The responsibility for the request bucket is passed to the request
510
* object. When the request is done with the bucket, it will be destroyed.
512
typedef apr_status_t (*serf_request_setup_t)(
513
serf_request_t *request,
515
serf_bucket_t **req_bkt,
516
serf_response_acceptor_t *acceptor,
517
void **acceptor_baton,
518
serf_response_handler_t *handler,
519
void **handler_baton,
523
* Construct a request object for the @a conn connection.
525
* When it is time to deliver the request, the @a setup callback will
526
* be invoked with the @a setup_baton passed into it to complete the
527
* construction of the request object.
529
* If the request has not (yet) been delivered, then it may be canceled
530
* with @see serf_request_cancel.
532
* Invoking any calls other than @see serf_request_cancel before the setup
533
* callback executes is not supported.
535
serf_request_t *serf_connection_request_create(
536
serf_connection_t *conn,
537
serf_request_setup_t setup,
541
* Construct a request object for the @a conn connection, add it in the
542
* list as the next to-be-written request before all unwritten requests.
544
* When it is time to deliver the request, the @a setup callback will
545
* be invoked with the @a setup_baton passed into it to complete the
546
* construction of the request object.
548
* If the request has not (yet) been delivered, then it may be canceled
549
* with @see serf_request_cancel.
551
* Invoking any calls other than @see serf_request_cancel before the setup
552
* callback executes is not supported.
554
serf_request_t *serf_connection_priority_request_create(
555
serf_connection_t *conn,
556
serf_request_setup_t setup,
560
* Cancel the request specified by the @a request object.
562
* If the request has been scheduled for delivery, then its response
563
* handler will be run, passing NULL for the response bucket.
565
* If the request has already been (partially or fully) delivered, then
566
* APR_EBUSY is returned and the request is *NOT* canceled. To properly
567
* cancel the request, the connection must be closed (by clearing or
568
* destroying its associated pool).
570
apr_status_t serf_request_cancel(
571
serf_request_t *request);
574
* Return the pool associated with @a request.
576
* WARNING: be very careful about the kinds of things placed into this
577
* pool. In particular, all allocation should be bounded in size, rather
578
* than proportional to any data stream.
580
apr_pool_t *serf_request_get_pool(
581
const serf_request_t *request);
584
* Return the bucket allocator associated with @a request.
586
serf_bucket_alloc_t *serf_request_get_alloc(
587
const serf_request_t *request);
590
* Return the connection associated with @a request.
592
serf_connection_t *serf_request_get_conn(
593
const serf_request_t *request);
596
* Update the @a handler and @a handler_baton for this @a request.
598
* This can be called after the request has started processing -
599
* subsequent data will be delivered to this new handler.
601
void serf_request_set_handler(
602
serf_request_t *request,
603
const serf_response_handler_t handler,
604
const void **handler_baton);
607
* Configure proxy server settings, to be used by all connections associated
608
* with the @a ctx serf context.
610
* The next connection will be created to connect to the proxy server
611
* specified by @a address. The address must live at least as long as the
614
void serf_config_proxy(
616
apr_sockaddr_t *address);
618
/* Supported authentication types. */
619
#define SERF_AUTHN_NONE 0x00
620
#define SERF_AUTHN_BASIC 0x01
621
#define SERF_AUTHN_DIGEST 0x02
622
#define SERF_AUTHN_NTLM 0x04
623
#define SERF_AUTHN_NEGOTIATE 0x08
624
#define SERF_AUTHN_ALL 0xFF
627
* Define the authentication handlers that serf will try on incoming requests.
629
void serf_config_authn_types(
634
* Set the credentials callback handler.
636
void serf_config_credentials_callback(
638
serf_credentials_callback_t cred_cb);
640
/* ### maybe some connection control functions for flood? */
642
/*** Special bucket creation functions ***/
645
* Create a bucket of type 'socket bucket'.
646
* This is basically a wrapper around @a serf_bucket_socket_create, which
647
* initializes the bucket using connection and/or context specific settings.
649
serf_bucket_t *serf_context_bucket_socket_create(
652
serf_bucket_alloc_t *allocator);
655
* Create a bucket of type 'request bucket'.
656
* This is basically a wrapper around @a serf_bucket_request_create, which
657
* initializes the bucket using request, connection and/or context specific
660
* This function will set following header(s):
661
* - Host: if the connection was created with @a serf_connection_create2.
663
serf_bucket_t *serf_request_bucket_request_create(
664
serf_request_t *request,
668
serf_bucket_alloc_t *allocator);
674
* @defgroup serf buckets
679
/** Pass as REQUESTED to the read function of a bucket to read, consume,
680
* and return all available data.
682
#define SERF_READ_ALL_AVAIL ((apr_size_t)-1)
684
/** Acceptable newline types for bucket->readline(). */
685
#define SERF_NEWLINE_CR 0x0001
686
#define SERF_NEWLINE_CRLF 0x0002
687
#define SERF_NEWLINE_LF 0x0004
688
#define SERF_NEWLINE_ANY 0x0007
690
/** Used to indicate that a newline is not present in the data buffer. */
691
/* ### should we make this zero? */
692
#define SERF_NEWLINE_NONE 0x0008
694
/** Used to indicate that a CR was found at the end of a buffer, and CRLF
695
* was acceptable. It may be that the LF is present, but it needs to be
698
* Note: an alternative to using this symbol would be for callers to see
699
* the SERF_NEWLINE_CR return value, and know that some "end of buffer" was
700
* reached. While this works well for @see serf_util_readline, it does not
701
* necessary work as well for buckets (there is no obvious "end of buffer",
702
* although there is an "end of bucket"). The other problem with that
703
* alternative is that developers might miss the condition. This symbol
704
* calls out the possibility and ensures that callers will watch for it.
706
#define SERF_NEWLINE_CRLF_SPLIT 0x0010
709
struct serf_bucket_type_t {
711
/** name of this bucket type */
715
* Read (and consume) up to @a requested bytes from @a bucket.
717
* A pointer to the data will be returned in @a data, and its length
718
* is specified by @a len.
720
* The data will exist until one of two conditions occur:
722
* 1) this bucket is destroyed
723
* 2) another call to any read function or to peek()
725
* If an application needs the data to exist for a longer duration,
726
* then it must make a copy.
728
apr_status_t (*read)(serf_bucket_t *bucket, apr_size_t requested,
729
const char **data, apr_size_t *len);
732
* Read (and consume) a line of data from @a bucket.
734
* The acceptable forms of a newline are given by @a acceptable, and
735
* the type found is returned in @a found. If a newline is not present
736
* in the returned data, then SERF_NEWLINE_NONE is stored into @a found.
738
* A pointer to the data is returned in @a data, and its length is
739
* specified by @a len. The data will include the newline, if present.
741
* Note that there is no way to limit the amount of data returned
744
* The lifetime of the data is the same as that of the @see read
747
apr_status_t (*readline)(serf_bucket_t *bucket, int acceptable,
749
const char **data, apr_size_t *len);
752
* Read a set of pointer/length pairs from the bucket.
754
* The size of the @a vecs array is specified by @a vecs_size. The
755
* bucket should fill in elements of the array, and return the number
756
* used in @a vecs_used.
758
* Each element of @a vecs should specify a pointer to a block of
759
* data and a length of that data.
761
* The total length of all data elements should not exceed the
762
* amount specified in @a requested.
764
* The lifetime of the data is the same as that of the @see read
767
apr_status_t (*read_iovec)(serf_bucket_t *bucket, apr_size_t requested,
768
int vecs_size, struct iovec *vecs,
772
* Read data from the bucket in a form suitable for apr_socket_sendfile()
774
* On input, hdtr->numheaders and hdtr->numtrailers specify the size
775
* of the hdtr->headers and hdtr->trailers arrays, respectively. The
776
* bucket should fill in the headers and trailers, up to the specified
777
* limits, and set numheaders and numtrailers to the number of iovecs
778
* filled in for each item.
780
* @a file should be filled in with a file that can be read. If a file
781
* is not available or appropriate, then NULL should be stored. The
782
* file offset for the data should be stored in @a offset, and the
783
* length of that data should be stored in @a len. If a file is not
784
* returned, then @a offset and @a len should be ignored.
786
* The file position is not required to correspond to @a offset, and
787
* the caller may manipulate it at will.
789
* The total length of all data elements, and the portion of the
790
* file should not exceed the amount specified in @a requested.
792
* The lifetime of the data is the same as that of the @see read
795
apr_status_t (*read_for_sendfile)(serf_bucket_t *bucket,
796
apr_size_t requested, apr_hdtr_t *hdtr,
797
apr_file_t **file, apr_off_t *offset,
801
* Look within @a bucket for a bucket of the given @a type. The bucket
802
* must be the "initial" data because it will be consumed by this
803
* function. If the given bucket type is available, then read and consume
804
* it, and return it to the caller.
806
* This function is usually used by readers that have custom handling
807
* for specific bucket types (e.g. looking for a file bucket to pass
808
* to apr_socket_sendfile).
810
* If a bucket of the given type is not found, then NULL is returned.
812
* The returned bucket becomes the responsibility of the caller. When
813
* the caller is done with the bucket, it should be destroyed.
815
serf_bucket_t * (*read_bucket)(serf_bucket_t *bucket,
816
const serf_bucket_type_t *type);
819
* Peek, but don't consume, the data in @a bucket.
821
* Since this function is non-destructive, the implicit read size is
822
* SERF_READ_ALL_AVAIL. The caller can then use whatever amount is
825
* The @a data parameter will point to the data, and @a len will
826
* specify how much data is available. The lifetime of the data follows
827
* the same rules as the @see read function above.
829
* Note: if the peek does not return enough data for your particular
830
* use, then you must read/consume some first, then peek again.
832
* If the returned data represents all available data, then APR_EOF
833
* will be returned. Since this function does not consume data, it
834
* can return the same data repeatedly rather than blocking; thus,
835
* APR_EAGAIN will never be returned.
837
apr_status_t (*peek)(serf_bucket_t *bucket,
838
const char **data, apr_size_t *len);
841
* Destroy @a bucket, along with any associated resources.
843
void (*destroy)(serf_bucket_t *bucket);
845
/* ### apr buckets have 'copy', 'split', and 'setaside' functions.
846
### not sure whether those will be needed in this bucket model.
851
* Should the use and lifecycle of buckets be tracked?
853
* When tracking, the system will ensure several semantic requirements
856
* - if a bucket returns APR_EAGAIN, one of its read functions should
857
* not be called immediately. the context's run loop should be called.
858
* ### and for APR_EOF, too?
859
* - all buckets must be drained of input before returning to the
860
* context's run loop.
861
* - buckets should not be destroyed before they return APR_EOF unless
862
* the connection is closed for some reason.
864
* Undefine this symbol to avoid the tracking (and a performance gain).
866
* ### we may want to examine when/how we provide this. should it always
867
* ### be compiled in? and apps select it before including this header?
869
/* #define SERF_DEBUG_BUCKET_USE */
872
/* Internal macros for tracking bucket use. */
873
#ifdef SERF_DEBUG_BUCKET_USE
874
#define SERF__RECREAD(b,s) serf_debug__record_read(b,s)
876
#define SERF__RECREAD(b,s) (s)
879
#define serf_bucket_read(b,r,d,l) SERF__RECREAD(b, (b)->type->read(b,r,d,l))
880
#define serf_bucket_readline(b,a,f,d,l) \
881
SERF__RECREAD(b, (b)->type->readline(b,a,f,d,l))
882
#define serf_bucket_read_iovec(b,r,s,v,u) \
883
SERF__RECREAD(b, (b)->type->read_iovec(b,r,s,v,u))
884
#define serf_bucket_read_for_sendfile(b,r,h,f,o,l) \
885
SERF__RECREAD(b, (b)->type->read_for_sendfile(b,r,h,f,o,l))
886
#define serf_bucket_read_bucket(b,t) ((b)->type->read_bucket(b,t))
887
#define serf_bucket_peek(b,d,l) ((b)->type->peek(b,d,l))
888
#define serf_bucket_destroy(b) ((b)->type->destroy(b))
891
* Check whether a real error occurred. Note that bucket read functions
892
* can return EOF and EAGAIN as part of their "normal" operation, so they
893
* should not be considered an error.
895
#define SERF_BUCKET_READ_ERROR(status) ((status) \
896
&& !APR_STATUS_IS_EOF(status) \
897
&& !APR_STATUS_IS_EAGAIN(status) \
898
&& (SERF_ERROR_WAIT_CONN != status))
901
struct serf_bucket_t {
903
/** the type of this bucket */
904
const serf_bucket_type_t *type;
906
/** bucket-private data */
909
/** the allocator used for this bucket (needed at destroy time) */
910
serf_bucket_alloc_t *allocator;
915
* Generic macro to construct "is TYPE" macros.
917
#define SERF_BUCKET_CHECK(b, btype) ((b)->type == &serf_bucket_type_ ## btype)
921
* Notification callback for a block that was not returned to the bucket
922
* allocator when its pool was destroyed.
924
* The block of memory is given by @a block. The baton provided when the
925
* allocator was constructed is passed as @a unfreed_baton.
927
typedef void (*serf_unfreed_func_t)(
932
* Create a new allocator for buckets.
934
* All buckets are associated with a serf bucket allocator. This allocator
935
* will be created within @a pool and will be destroyed when that pool is
936
* cleared or destroyed.
938
* When the allocator is destroyed, if any allocations were not explicitly
939
* returned (by calling serf_bucket_mem_free), then the @a unfreed callback
940
* will be invoked for each block. @a unfreed_baton will be passed to the
943
* If @a unfreed is NULL, then the library will invoke the abort() stdlib
944
* call. Any failure to return memory is a bug in the application, and an
945
* abort can assist with determining what kinds of memory were not freed.
947
serf_bucket_alloc_t *serf_bucket_allocator_create(
949
serf_unfreed_func_t unfreed,
950
void *unfreed_baton);
953
* Return the pool that was used for this @a allocator.
955
* WARNING: the use of this pool for allocations requires a very
956
* detailed understanding of pool behaviors, the bucket system,
957
* and knowledge of the bucket's use within the overall pattern
958
* of request/response behavior.
960
* See design-guide.txt for more information about pool usage.
962
apr_pool_t *serf_bucket_allocator_get_pool(
963
const serf_bucket_alloc_t *allocator);
967
* Utility structure for reading a complete line of input from a bucket.
969
* Since it is entirely possible for a line to be broken by APR_EAGAIN,
970
* this structure can be used to accumulate the data until a complete line
971
* has been read from a bucket.
974
/* This limit applies to the line buffer functions. If an application needs
975
* longer lines, then they will need to manually handle line buffering.
977
#define SERF_LINEBUF_LIMIT 8000
981
/* Current state of the buffer. */
985
SERF_LINEBUF_PARTIAL,
986
SERF_LINEBUF_CRLF_SPLIT
989
/* How much of the buffer have we used? */
992
/* The line is read into this buffer, minus CR/LF */
993
char line[SERF_LINEBUF_LIMIT];
998
* Initialize the @a linebuf structure.
1000
void serf_linebuf_init(serf_linebuf_t *linebuf);
1003
* Fetch a line of text from @a bucket, accumulating the line into
1004
* @a linebuf. @a acceptable specifies the types of newlines which are
1005
* acceptable for this fetch.
1007
* ### we should return a data/len pair so that we can avoid a copy,
1008
* ### rather than having callers look into our state and line buffer.
1010
apr_status_t serf_linebuf_fetch(
1011
serf_linebuf_t *linebuf,
1012
serf_bucket_t *bucket,
1018
/* Internal functions for bucket use and lifecycle tracking */
1019
apr_status_t serf_debug__record_read(
1020
const serf_bucket_t *bucket,
1021
apr_status_t status);
1022
void serf_debug__entered_loop(
1023
serf_bucket_alloc_t *allocator);
1024
void serf_debug__closed_conn(
1025
serf_bucket_alloc_t *allocator);
1026
void serf_debug__bucket_destroy(
1027
const serf_bucket_t *bucket);
1028
void serf_debug__bucket_alloc_check(
1029
serf_bucket_alloc_t *allocator);
1032
#define SERF_MAJOR_VERSION 1
1033
#define SERF_MINOR_VERSION 1
1034
#define SERF_PATCH_VERSION 0
1036
/* Version number string */
1037
#define SERF_VERSION_STRING APR_STRINGIFY(SERF_MAJOR_VERSION) "." \
1038
APR_STRINGIFY(SERF_MINOR_VERSION) "." \
1039
APR_STRINGIFY(SERF_PATCH_VERSION)
1042
* Check at compile time if the Serf version is at least a certain
1044
* @param major The major version component of the version checked
1045
* for (e.g., the "1" of "1.3.0").
1046
* @param minor The minor version component of the version checked
1047
* for (e.g., the "3" of "1.3.0").
1048
* @param patch The patch level component of the version checked
1049
* for (e.g., the "0" of "1.3.0").
1051
#define SERF_VERSION_AT_LEAST(major,minor,patch) \
1052
(((major) < SERF_MAJOR_VERSION) \
1053
|| ((major) == SERF_MAJOR_VERSION && (minor) < SERF_MINOR_VERSION) \
1054
|| ((major) == SERF_MAJOR_VERSION && (minor) == SERF_MINOR_VERSION && \
1055
(patch) <= SERF_PATCH_VERSION))
1059
* Returns the version of the library the application has linked/loaded.
1060
* Values are returned in @a major, @a minor, and @a patch.
1062
* Applications will want to use this function to verify compatibility,
1063
* expecially while serf has not reached a 1.0 milestone. APIs and
1064
* semantics may change drastically until the library hits 1.0.
1066
void serf_lib_version(
1078
* Every user of serf will want to deal with our various bucket types.
1079
* Go ahead and include that header right now.
1081
* Note: make sure this occurs outside of the C++ namespace block
1083
#include "serf_bucket_types.h"
1086
#endif /* !SERF_H */