1
/* Licensed to the Apache Software Foundation (ASF) under one or more
2
* contributor license agreements. See the NOTICE file distributed with
3
* this work for additional information regarding copyright ownership.
4
* The ASF licenses this file to You under the Apache License, Version 2.0
5
* (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
18
* @file http_protocol.h
19
* @brief HTTP protocol handling
21
* @defgroup APACHE_CORE_PROTO HTTP Protocol Handling
22
* @ingroup APACHE_CORE
26
#ifndef APACHE_HTTP_PROTOCOL_H
27
#define APACHE_HTTP_PROTOCOL_H
30
#include "apr_hooks.h"
31
#include "apr_portable.h"
33
#include "apr_buckets.h"
34
#include "util_filter.h"
41
* This hook allows modules to insert filters for the current error response
42
* @param r the current request
45
AP_DECLARE_HOOK(void,insert_error_filter,(request_rec *r))
47
/** This is an optimization. We keep a record of the filter_rec that
48
* stores the old_write filter, so that we can avoid strcmp's later.
50
AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
53
* Prototypes for routines which either talk directly back to the user,
54
* or control the ones that eventually do.
58
* Read a request and fill in the fields.
59
* @param c The current connection
60
* @return The new request_rec
62
request_rec *ap_read_request(conn_rec *c);
65
* Read the mime-encoded headers.
66
* @param r The current request
68
AP_DECLARE(void) ap_get_mime_headers(request_rec *r);
71
* Optimized version of ap_get_mime_headers() that requires a
72
* temporary brigade to work with
73
* @param r The current request
74
* @param bb temp brigade
76
AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r,
77
apr_bucket_brigade *bb);
79
/* Finish up stuff after a request */
82
* Called at completion of sending the response. It sends the terminating
83
* protocol information.
84
* @param r The current request
86
AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
89
* Send error back to client.
90
* @param r The current request
91
* @param recursive_error last arg indicates error status in case we get
92
* an error in the process of trying to deal with an ErrorDocument
93
* to handle some other error. In that case, we print the default
94
* report for the first thing that went wrong, and more briefly report
95
* on the problem with the ErrorDocument.
97
AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
99
/* Set last modified header line from the lastmod date of the associated file.
100
* Also, set content length.
102
* May return an error status, typically HTTP_NOT_MODIFIED (that when the
103
* permit_cache argument is set to one).
107
* Set the content length for this request
108
* @param r The current request
109
* @param length The new content length
111
AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
114
* Set the keepalive status for this request
115
* @param r The current request
116
* @return 1 if keepalive can be set, 0 otherwise
118
AP_DECLARE(int) ap_set_keepalive(request_rec *r);
121
* Return the latest rational time from a request/mtime pair. Mtime is
122
* returned unless it's in the future, in which case we return the current time.
123
* @param r The current request
124
* @param mtime The last modified time
125
* @return the latest rational time.
127
AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
130
* Build the content-type that should be sent to the client from the
131
* content-type specified. The following rules are followed:
132
* - if type is NULL, type is set to ap_default_type(r)
133
* - if charset adding is disabled, stop processing and return type.
134
* - then, if there are no parameters on type, add the default charset
136
* @param r The current request
137
* @param type The content type
138
* @return The content-type
140
AP_DECLARE(const char *) ap_make_content_type(request_rec *r,
145
* Precompile metadata structures used by ap_make_content_type()
146
* @param pool The pool to use for allocations
148
AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool);
149
#endif /* CORE_PRIVATE */
152
* Construct an entity tag from the resource information. If it's a real
153
* file, build in some of the file characteristics.
154
* @param r The current request
155
* @param force_weak Force the entity tag to be weak - it could be modified
156
* again in as short an interval.
157
* @return The entity tag
159
AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
162
* Set the E-tag outgoing header
163
* @param r The current request
165
AP_DECLARE(void) ap_set_etag(request_rec *r);
168
* Set the last modified time for the file being sent
169
* @param r The current request
171
AP_DECLARE(void) ap_set_last_modified(request_rec *r);
174
* Implements condition GET rules for HTTP/1.1 specification. This function
175
* inspects the client headers and determines if the response fulfills
176
* the requirements specified.
177
* @param r The current request
178
* @return OK if the response fulfills the condition GET rules, some
179
* other status code otherwise
181
AP_DECLARE(int) ap_meets_conditions(request_rec *r);
183
/* Other ways to send stuff at the client. All of these keep track
184
* of bytes_sent automatically. This indirection is intended to make
185
* it a little more painless to slide things like HTTP-NG packetization
186
* underneath the main body of the code later. In the meantime, it lets
187
* us centralize a bit of accounting (bytes_sent).
189
* These also return the number of bytes written by the call.
190
* They should only be called with a timeout registered, for obvious reaasons.
191
* (Ditto the send_header stuff).
195
* Send an entire file to the client, using sendfile if supported by the
197
* @param fd The file to send.
198
* @param r The current request
199
* @param offset Offset into the file to start sending.
200
* @param length Amount of data to send
201
* @param nbytes Amount of data actually sent
203
AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
204
apr_size_t length, apr_size_t *nbytes);
208
* Send an MMAP'ed file to the client
209
* @param mm The MMAP'ed file to send
210
* @param r The current request
211
* @param offset The offset into the MMAP to start sending
212
* @param length The amount of data to send
213
* @return The number of bytes sent
215
AP_DECLARE(size_t) ap_send_mmap(apr_mmap_t *mm, request_rec *r, size_t offset,
221
* Register a new request method, and return the offset that will be
222
* associated with that method.
224
* @param p The pool to create registered method numbers from.
225
* @param methname The name of the new method to register.
226
* @return Ab int value representing an offset into a bitmask.
228
AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
231
* Initialize the method_registry and allocate memory for it.
233
* @param p Pool to allocate memory for the registry from.
235
AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
238
* This is a convenience macro to ease with checking a mask
239
* against a method name.
241
#define AP_METHOD_CHECK_ALLOWED(mask, methname) \
242
((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
245
* Create a new method list with the specified number of preallocated
246
* slots for extension methods.
248
* @param p Pointer to a pool in which the structure should be
250
* @param nelts Number of preallocated extension slots
251
* @return Pointer to the newly created structure.
253
AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
259
* @param dest List to copy to
260
* @param src List to copy from
262
AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
263
ap_method_list_t *src);
266
* Search for an HTTP method name in an ap_method_list_t structure, and
267
* return true if found.
269
* @param method String containing the name of the method to check.
270
* @param l Pointer to a method list, such as cmd->methods_limited.
271
* @return 1 if method is in the list, otherwise 0
273
AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
276
* Add an HTTP method name to an ap_method_list_t structure if it isn't
279
* @param method String containing the name of the method to check.
280
* @param l Pointer to a method list, such as cmd->methods_limited.
283
AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
286
* Remove an HTTP method name from an ap_method_list_t structure.
288
* @param l Pointer to a method list, such as cmd->methods_limited.
289
* @param method String containing the name of the method to remove.
292
AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
296
* Reset a method list to be completely empty.
298
* @param l Pointer to a method list, such as cmd->methods_limited.
301
AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
304
* Set the content type for this request (r->content_type).
305
* @param r The current request
306
* @param ct The new content type
307
* @warning This function must be called to set r->content_type in order
308
* for the AddOutputFilterByType directive to work correctly.
310
AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
312
/* Hmmm... could macrofy these for now, and maybe forever, though the
313
* definitions of the macros would get a whole lot hairier.
317
* Output one character for this request
318
* @param c the character to output
319
* @param r the current request
320
* @return The number of bytes sent
322
AP_DECLARE(int) ap_rputc(int c, request_rec *r);
325
* Output a string for the current request
326
* @param str The string to output
327
* @param r The current request
328
* @return The number of bytes sent
330
AP_DECLARE(int) ap_rputs(const char *str, request_rec *r);
333
* Write a buffer for the current request
334
* @param buf The buffer to write
335
* @param nbyte The number of bytes to send from the buffer
336
* @param r The current request
337
* @return The number of bytes sent
339
AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
342
* Write an unspecified number of strings to the request
343
* @param r The current request
344
* @param ... The strings to write
345
* @return The number of bytes sent
347
AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...);
350
* Output data to the client in a printf format
351
* @param r The current request
352
* @param fmt The format string
353
* @param vlist The arguments to use to fill out the format string
354
* @return The number of bytes sent
356
AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
359
* Output data to the client in a printf format
360
* @param r The current request
361
* @param fmt The format string
362
* @param ... The arguments to use to fill out the format string
363
* @return The number of bytes sent
365
AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
366
__attribute__((format(printf,2,3)));
369
* Flush all of the data for the current request to the client
370
* @param r The current request
371
* @return The number of bytes sent
373
AP_DECLARE(int) ap_rflush(request_rec *r);
376
* Index used in custom_responses array for a specific error code
377
* (only use outside protocol.c is in getting them configured).
378
* @param status HTTP status code
379
* @return The index of the response
381
AP_DECLARE(int) ap_index_of_response(int status);
384
* Return the Status-Line for a given status code (excluding the
385
* HTTP-Version field). If an invalid or unknown status code is
386
* passed, "500 Internal Server Error" will be returned.
387
* @param status The HTTP status code
388
* @return The Status-Line
390
AP_DECLARE(const char *) ap_get_status_line(int status);
392
/* Reading a block of data from the client connection (e.g., POST arg) */
395
* Setup the client to allow Apache to read the request body.
396
* @param r The current request
397
* @param read_policy How the server should interpret a chunked
398
* transfer-encoding. One of: <pre>
399
* REQUEST_NO_BODY Send 413 error if message has any body
400
* REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
401
* REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
403
* @return either OK or an error code
405
AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
408
* Determine if the client has sent any data. This also sends a
409
* 100 Continue response to HTTP/1.1 clients, so modules should not be called
410
* until the module is ready to read content.
411
* @warning Never call this function more than once.
412
* @param r The current request
413
* @return 0 if there is no message to read, 1 otherwise
415
AP_DECLARE(int) ap_should_client_block(request_rec *r);
418
* Call this in a loop. It will put data into a buffer and return the length
420
* @param r The current request
421
* @param buffer The buffer in which to store the data
422
* @param bufsiz The size of the buffer
423
* @return Number of bytes inserted into the buffer. When done reading, 0
424
* if EOF, or -1 if there was an error
426
AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
429
* In HTTP/1.1, any method can have a body. However, most GET handlers
430
* wouldn't know what to do with a request body if they received one.
431
* This helper routine tests for and reads any message body in the request,
432
* simply discarding whatever it receives. We need to do this because
433
* failing to read the request body would cause it to be interpreted
434
* as the next request on a persistent connection.
435
* @param r The current request
436
* @return error status if request is malformed, OK otherwise
438
AP_DECLARE(int) ap_discard_request_body(request_rec *r);
441
* Setup the output headers so that the client knows how to authenticate
442
* itself the next time, if an authentication request failed. This function
443
* works for both basic and digest authentication
444
* @param r The current request
446
AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
449
* Setup the output headers so that the client knows how to authenticate
450
* itself the next time, if an authentication request failed. This function
451
* works only for basic authentication
452
* @param r The current request
454
AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
457
* Setup the output headers so that the client knows how to authenticate
458
* itself the next time, if an authentication request failed. This function
459
* works only for digest authentication
460
* @param r The current request
462
AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
465
* Get the password from the request headers
466
* @param r The current request
467
* @param pw The password as set in the headers
468
* @return 0 (OK) if it set the 'pw' argument (and assured
469
* a correct value in r->user); otherwise it returns
470
* an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
471
* really confused, HTTP_UNAUTHORIZED if no authentication at all
472
* seemed to be in use, or DECLINED if there was authentication but
473
* it wasn't Basic (in which case, the caller should presumably
476
AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
479
* parse_uri: break apart the uri
480
* @warning Side Effects:
481
* @li sets r->args to rest after '?' (or NULL if no '?')
482
* @li sets r->uri to request uri (without r->args part)
483
* @li sets r->hostname (if not set already) from request (scheme://host:port)
484
* @param r The current request
485
* @param uri The uri to break apart
487
AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
490
* Get the next line of input for the request
491
* @param s The buffer into which to read the line
492
* @param n The size of the buffer
493
* @param r The request
494
* @param fold Whether to merge continuation lines
495
* @return The length of the line, if successful
496
* n, if the line is too big to fit in the buffer
497
* -1 for miscellaneous errors
499
AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int fold);
502
* Get the next line of input for the request
504
* Note: on ASCII boxes, ap_rgetline is a macro which simply calls
505
* ap_rgetline_core to get the line of input.
507
* on EBCDIC boxes, ap_rgetline is a wrapper function which
508
* translates ASCII protocol lines to the local EBCDIC code page
509
* after getting the line of input.
511
* @param s Pointer to the pointer to the buffer into which the line
512
* should be read; if *s==NULL, a buffer of the necessary size
513
* to hold the data will be allocated from the request pool
514
* @param n The size of the buffer
515
* @param read The length of the line.
516
* @param r The request
517
* @param fold Whether to merge continuation lines
518
* @param bb Working brigade to use when reading buckets
519
* @return APR_SUCCESS, if successful
520
* APR_ENOSPC, if the line is too big to fit in the buffer
521
* Other errors where appropriate
523
#if APR_CHARSET_EBCDIC
524
AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
526
request_rec *r, int fold,
527
apr_bucket_brigade *bb);
528
#else /* ASCII box */
529
#define ap_rgetline(s, n, read, r, fold, bb) \
530
ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
533
/** @see ap_rgetline */
534
AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
536
request_rec *r, int fold,
537
apr_bucket_brigade *bb);
540
* Get the method number associated with the given string, assumed to
541
* contain an HTTP method. Returns M_INVALID if not recognized.
542
* @param method A string containing a valid HTTP method
543
* @return The method number
545
AP_DECLARE(int) ap_method_number_of(const char *method);
548
* Get the method name associated with the given internal method
549
* number. Returns NULL if not recognized.
550
* @param p A pool to use for temporary allocations.
551
* @param methnum An integer value corresponding to an internal method number
552
* @return The name corresponding to the method number
554
AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
559
* post_read_request --- run right after read_request or internal_redirect,
560
* and not run during any subrequests.
563
* This hook allows modules to affect the request immediately after the request
564
* has been read, and before any other phases have been processes. This allows
565
* modules to make decisions based upon the input header fields
566
* @param r The current request
567
* @return OK or DECLINED
569
AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
572
* This hook allows modules to perform any module-specific logging activities
573
* over and above the normal server things.
574
* @param r The current request
575
* @return OK, DECLINED, or HTTP_...
577
AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
580
* This hook allows modules to retrieve the http scheme for a request. This
581
* allows Apache modules to easily extend the schemes that Apache understands
582
* @param r The current request
583
* @return The http scheme from the request
585
AP_DECLARE_HOOK(const char *,http_scheme,(const request_rec *r))
588
* Return the default port from the current request
589
* @param r The current request
590
* @return The current port
592
AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
594
/** @see ap_bucket_type_error */
595
typedef struct ap_bucket_error ap_bucket_error;
598
* @struct ap_bucket_error
599
* @brief A bucket referring to an HTTP error
601
* This bucket can be passed down the filter stack to indicate that an
602
* HTTP error occurred while running a filter. In order for this bucket
603
* to be used successfully, it MUST be sent as the first bucket in the
604
* first brigade to be sent from a given filter.
606
struct ap_bucket_error {
607
/** Number of buckets using this memory */
608
apr_bucket_refcount refcount;
609
/** The error code */
611
/** The error string */
615
/** @see ap_bucket_type_error */
616
AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
619
* Determine if a bucket is an error bucket
620
* @param e The bucket to inspect
621
* @return true or false
623
#define AP_BUCKET_IS_ERROR(e) (e->type == &ap_bucket_type_error)
626
* Make the bucket passed in an error bucket
627
* @param b The bucket to make into an error bucket
628
* @param error The HTTP error code to put in the bucket.
629
* @param buf An optional error string to put in the bucket.
630
* @param p A pool to allocate out of.
631
* @return The new bucket, or NULL if allocation failed
633
AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
634
const char *buf, apr_pool_t *p);
637
* Create a bucket referring to an HTTP error.
638
* @param error The HTTP error code to put in the bucket.
639
* @param buf An optional error string to put in the bucket.
640
* @param p A pool to allocate the error string out of.
641
* @param list The bucket allocator from which to allocate the bucket
642
* @return The new bucket, or NULL if allocation failed
644
AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
646
apr_bucket_alloc_t *list);
648
AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
649
AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
650
AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
651
apr_bucket_brigade *);
652
AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
655
* Sett up the protocol fields for subsidiary requests
656
* @param rnew New Sub Request
657
* @param r current request
659
AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
662
* A wrapup function to keep the internal accounting straight.
663
* Indicates that there is no more content coming.
664
* @param sub_r Subrequest that is now compete
666
AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
672
#endif /* !APACHE_HTTP_PROTOCOL_H */