http_protocol.h 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023
  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
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  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.
  15. */
  16. /**
  17. * @file http_protocol.h
  18. * @brief HTTP protocol handling
  19. *
  20. * @defgroup APACHE_CORE_PROTO HTTP Protocol Handling
  21. * @ingroup APACHE_CORE
  22. * @{
  23. */
  24. #ifndef APACHE_HTTP_PROTOCOL_H
  25. #define APACHE_HTTP_PROTOCOL_H
  26. #include "httpd.h"
  27. #include "apr_portable.h"
  28. #include "apr_mmap.h"
  29. #include "apr_buckets.h"
  30. #include "util_filter.h"
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /**
  35. * This hook allows modules to insert filters for the current error response
  36. * @param r the current request
  37. * @ingroup hooks
  38. */
  39. AP_DECLARE_HOOK(void,insert_error_filter,(request_rec *r))
  40. /** This is an optimization. We keep a record of the filter_rec that
  41. * stores the old_write filter, so that we can avoid strcmp's later.
  42. */
  43. AP_DECLARE_DATA extern ap_filter_rec_t *ap_old_write_func;
  44. /*
  45. * Prototypes for routines which either talk directly back to the user,
  46. * or control the ones that eventually do.
  47. */
  48. /**
  49. * Read a request and fill in the fields.
  50. * @param c The current connection
  51. * @return The new request_rec
  52. */
  53. request_rec *ap_read_request(conn_rec *c);
  54. /**
  55. * Read the mime-encoded headers.
  56. * @param r The current request
  57. */
  58. AP_DECLARE(void) ap_get_mime_headers(request_rec *r);
  59. /**
  60. * Optimized version of ap_get_mime_headers() that requires a
  61. * temporary brigade to work with
  62. * @param r The current request
  63. * @param bb temp brigade
  64. */
  65. AP_DECLARE(void) ap_get_mime_headers_core(request_rec *r,
  66. apr_bucket_brigade *bb);
  67. /* Finish up stuff after a request */
  68. /**
  69. * Called at completion of sending the response. It sends the terminating
  70. * protocol information.
  71. * @param r The current request
  72. */
  73. AP_DECLARE(void) ap_finalize_request_protocol(request_rec *r);
  74. /**
  75. * Send error back to client.
  76. * @param r The current request
  77. * @param recursive_error last arg indicates error status in case we get
  78. * an error in the process of trying to deal with an ErrorDocument
  79. * to handle some other error. In that case, we print the default
  80. * report for the first thing that went wrong, and more briefly report
  81. * on the problem with the ErrorDocument.
  82. */
  83. AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);
  84. /* Set last modified header line from the lastmod date of the associated file.
  85. * Also, set content length.
  86. *
  87. * May return an error status, typically HTTP_NOT_MODIFIED (that when the
  88. * permit_cache argument is set to one).
  89. */
  90. /**
  91. * Set the content length for this request
  92. * @param r The current request
  93. * @param length The new content length
  94. */
  95. AP_DECLARE(void) ap_set_content_length(request_rec *r, apr_off_t length);
  96. /**
  97. * Set the keepalive status for this request
  98. * @param r The current request
  99. * @return 1 if keepalive can be set, 0 otherwise
  100. */
  101. AP_DECLARE(int) ap_set_keepalive(request_rec *r);
  102. /**
  103. * Return the latest rational time from a request/mtime pair. Mtime is
  104. * returned unless it's in the future, in which case we return the current time.
  105. * @param r The current request
  106. * @param mtime The last modified time
  107. * @return the latest rational time.
  108. */
  109. AP_DECLARE(apr_time_t) ap_rationalize_mtime(request_rec *r, apr_time_t mtime);
  110. /**
  111. * Build the content-type that should be sent to the client from the
  112. * content-type specified. The following rules are followed:
  113. * - if type is NULL or "", return NULL (do not set content-type).
  114. * - if charset adding is disabled, stop processing and return type.
  115. * - then, if there are no parameters on type, add the default charset
  116. * - return type
  117. * @param r The current request
  118. * @param type The content type
  119. * @return The content-type
  120. */
  121. AP_DECLARE(const char *) ap_make_content_type(request_rec *r,
  122. const char *type);
  123. /**
  124. * Precompile metadata structures used by ap_make_content_type()
  125. * @param pool The pool to use for allocations
  126. */
  127. AP_DECLARE(void) ap_setup_make_content_type(apr_pool_t *pool);
  128. /**
  129. * Construct an entity tag from the resource information. If it's a real
  130. * file, build in some of the file characteristics.
  131. * @param r The current request
  132. * @param force_weak Force the entity tag to be weak - it could be modified
  133. * again in as short an interval.
  134. * @return The entity tag
  135. */
  136. AP_DECLARE(char *) ap_make_etag(request_rec *r, int force_weak);
  137. /**
  138. * Set the E-tag outgoing header
  139. * @param r The current request
  140. */
  141. AP_DECLARE(void) ap_set_etag(request_rec *r);
  142. /**
  143. * Set the last modified time for the file being sent
  144. * @param r The current request
  145. */
  146. AP_DECLARE(void) ap_set_last_modified(request_rec *r);
  147. typedef enum {
  148. AP_CONDITION_NONE,
  149. AP_CONDITION_NOMATCH,
  150. AP_CONDITION_WEAK,
  151. AP_CONDITION_STRONG
  152. } ap_condition_e;
  153. /**
  154. * Tests conditional request rules for the If-Match header.
  155. * @param r The current request
  156. * @param headers The response headers to check against
  157. * @return AP_CONDITION_NONE if the header is missing, AP_CONDITION_NOMATCH
  158. * if the header does not match, AP_CONDITION_STRONG for a strong
  159. * match. Weak matches are not permitted for the If-Match header.
  160. */
  161. AP_DECLARE(ap_condition_e) ap_condition_if_match(request_rec *r,
  162. apr_table_t *headers);
  163. /**
  164. * Tests conditional request rules for the If-Unmodified-Since header.
  165. * @param r The current request
  166. * @param headers The response headers to check against
  167. * @return AP_CONDITION_NONE if the header is missing, AP_CONDITION_NOMATCH
  168. * if the header does not match, AP_CONDITION_WEAK if a weak match
  169. * was present and allowed by RFC2616, AP_CONDITION_STRONG for a
  170. * strong match.
  171. */
  172. AP_DECLARE(ap_condition_e) ap_condition_if_unmodified_since(request_rec *r,
  173. apr_table_t *headers);
  174. /**
  175. * Tests conditional request rules for the If-None-Match header.
  176. * @param r The current request
  177. * @param headers The response headers to check against
  178. * @return AP_CONDITION_NONE if the header is missing, AP_CONDITION_NOMATCH
  179. * if the header does not match, AP_CONDITION_WEAK if a weak match
  180. * was present and allowed by RFC2616, AP_CONDITION_STRONG for a
  181. * strong match.
  182. */
  183. AP_DECLARE(ap_condition_e) ap_condition_if_none_match(request_rec *r,
  184. apr_table_t *headers);
  185. /**
  186. * Tests conditional request rules for the If-Modified-Since header.
  187. * @param r The current request
  188. * @param headers The response headers to check against
  189. * @return AP_CONDITION_NONE if the header is missing, AP_CONDITION_NOMATCH
  190. * if the header does not match, AP_CONDITION_WEAK if a weak match
  191. * was present and allowed by RFC2616, AP_CONDITION_STRONG for a
  192. * strong match.
  193. */
  194. AP_DECLARE(ap_condition_e) ap_condition_if_modified_since(request_rec *r,
  195. apr_table_t *headers);
  196. /**
  197. * Tests conditional request rules for the If-Range header.
  198. * @param r The current request
  199. * @param headers The response headers to check against
  200. * @return AP_CONDITION_NONE if either the If-Range or Range header is
  201. * missing, AP_CONDITION_NOMATCH if the header does not match,
  202. * AP_CONDITION_STRONG for a strong match. Weak matches are not
  203. * permitted for the If-Range header.
  204. */
  205. AP_DECLARE(ap_condition_e) ap_condition_if_range(request_rec *r,
  206. apr_table_t *headers);
  207. /**
  208. * Implements condition GET rules for HTTP/1.1 specification. This function
  209. * inspects the client headers and determines if the response fulfills
  210. * the requirements specified.
  211. * @param r The current request
  212. * @return OK if the response fulfills the condition GET rules, some
  213. * other status code otherwise
  214. */
  215. AP_DECLARE(int) ap_meets_conditions(request_rec *r);
  216. /* Other ways to send stuff at the client. All of these keep track
  217. * of bytes_sent automatically. This indirection is intended to make
  218. * it a little more painless to slide things like HTTP-NG packetization
  219. * underneath the main body of the code later. In the meantime, it lets
  220. * us centralize a bit of accounting (bytes_sent).
  221. *
  222. * These also return the number of bytes written by the call.
  223. * They should only be called with a timeout registered, for obvious reaasons.
  224. * (Ditto the send_header stuff).
  225. */
  226. /**
  227. * Send an entire file to the client, using sendfile if supported by the
  228. * current platform
  229. * @param fd The file to send.
  230. * @param r The current request
  231. * @param offset Offset into the file to start sending.
  232. * @param length Amount of data to send
  233. * @param nbytes Amount of data actually sent
  234. */
  235. AP_DECLARE(apr_status_t) ap_send_fd(apr_file_t *fd, request_rec *r, apr_off_t offset,
  236. apr_size_t length, apr_size_t *nbytes);
  237. #if APR_HAS_MMAP
  238. /**
  239. * Send an MMAP'ed file to the client
  240. * @param mm The MMAP'ed file to send
  241. * @param r The current request
  242. * @param offset The offset into the MMAP to start sending
  243. * @param length The amount of data to send
  244. * @return The number of bytes sent
  245. */
  246. AP_DECLARE(apr_size_t) ap_send_mmap(apr_mmap_t *mm,
  247. request_rec *r,
  248. apr_size_t offset,
  249. apr_size_t length);
  250. #endif
  251. /**
  252. * Register a new request method, and return the offset that will be
  253. * associated with that method.
  254. *
  255. * @param p The pool to create registered method numbers from.
  256. * @param methname The name of the new method to register.
  257. * @return An int value representing an offset into a bitmask.
  258. */
  259. AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname);
  260. /**
  261. * Initialize the method_registry and allocate memory for it.
  262. *
  263. * @param p Pool to allocate memory for the registry from.
  264. */
  265. AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p);
  266. /**
  267. * This is a convenience macro to ease with checking a mask
  268. * against a method name.
  269. */
  270. #define AP_METHOD_CHECK_ALLOWED(mask, methname) \
  271. ((mask) & (AP_METHOD_BIT << ap_method_number_of((methname))))
  272. /**
  273. * Create a new method list with the specified number of preallocated
  274. * slots for extension methods.
  275. *
  276. * @param p Pointer to a pool in which the structure should be
  277. * allocated.
  278. * @param nelts Number of preallocated extension slots
  279. * @return Pointer to the newly created structure.
  280. */
  281. AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts);
  282. /**
  283. * Copy a method list
  284. *
  285. * @param dest List to copy to
  286. * @param src List to copy from
  287. */
  288. AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
  289. ap_method_list_t *src);
  290. /**
  291. * Search for an HTTP method name in an ap_method_list_t structure, and
  292. * return true if found.
  293. *
  294. * @param method String containing the name of the method to check.
  295. * @param l Pointer to a method list, such as r->allowed_methods.
  296. * @return 1 if method is in the list, otherwise 0
  297. */
  298. AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method);
  299. /**
  300. * Add an HTTP method name to an ap_method_list_t structure if it isn't
  301. * already listed.
  302. *
  303. * @param method String containing the name of the method to check.
  304. * @param l Pointer to a method list, such as r->allowed_methods.
  305. * @return None.
  306. */
  307. AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method);
  308. /**
  309. * Remove an HTTP method name from an ap_method_list_t structure.
  310. *
  311. * @param l Pointer to a method list, such as r->allowed_methods.
  312. * @param method String containing the name of the method to remove.
  313. * @return None.
  314. */
  315. AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
  316. const char *method);
  317. /**
  318. * Reset a method list to be completely empty.
  319. *
  320. * @param l Pointer to a method list, such as r->allowed_methods.
  321. * @return None.
  322. */
  323. AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l);
  324. /**
  325. * Set the content type for this request (r->content_type).
  326. * @param r The current request
  327. * @param ct The new content type
  328. * @warning This function must be called to set r->content_type in order
  329. * for the AddOutputFilterByType directive to work correctly.
  330. */
  331. AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct);
  332. /**
  333. * Set the Accept-Ranges header for this response
  334. * @param r The current request
  335. */
  336. AP_DECLARE(void) ap_set_accept_ranges(request_rec *r);
  337. /* Hmmm... could macrofy these for now, and maybe forever, though the
  338. * definitions of the macros would get a whole lot hairier.
  339. */
  340. /**
  341. * Output one character for this request
  342. * @param c the character to output
  343. * @param r the current request
  344. * @return The number of bytes sent
  345. */
  346. AP_DECLARE(int) ap_rputc(int c, request_rec *r);
  347. /**
  348. * Write a buffer for the current request
  349. * @param buf The buffer to write
  350. * @param nbyte The number of bytes to send from the buffer
  351. * @param r The current request
  352. * @return The number of bytes sent
  353. */
  354. AP_DECLARE(int) ap_rwrite(const void *buf, int nbyte, request_rec *r);
  355. /**
  356. * Output a string for the current request
  357. * @param str The string to output
  358. * @param r The current request
  359. * @return The number of bytes sent
  360. * @note ap_rputs may be implemented as macro or inline function
  361. */
  362. static APR_INLINE int ap_rputs(const char *str, request_rec *r)
  363. {
  364. return ap_rwrite(str, (int)strlen(str), r);
  365. }
  366. /**
  367. * Write an unspecified number of strings to the request
  368. * @param r The current request
  369. * @param ... The strings to write
  370. * @return The number of bytes sent
  371. */
  372. AP_DECLARE_NONSTD(int) ap_rvputs(request_rec *r,...)
  373. AP_FN_ATTR_SENTINEL;
  374. /**
  375. * Output data to the client in a printf format
  376. * @param r The current request
  377. * @param fmt The format string
  378. * @param vlist The arguments to use to fill out the format string
  379. * @return The number of bytes sent
  380. */
  381. AP_DECLARE(int) ap_vrprintf(request_rec *r, const char *fmt, va_list vlist);
  382. /**
  383. * Output data to the client in a printf format
  384. * @param r The current request
  385. * @param fmt The format string
  386. * @param ... The arguments to use to fill out the format string
  387. * @return The number of bytes sent
  388. */
  389. AP_DECLARE_NONSTD(int) ap_rprintf(request_rec *r, const char *fmt,...)
  390. __attribute__((format(printf,2,3)));
  391. /**
  392. * Flush all of the data for the current request to the client
  393. * @param r The current request
  394. * @return 0 on success, -1 if an error occurred
  395. */
  396. AP_DECLARE(int) ap_rflush(request_rec *r);
  397. /**
  398. * Index used in custom_responses array for a specific error code
  399. * (only use outside protocol.c is in getting them configured).
  400. * @param status HTTP status code
  401. * @return The index of the response
  402. */
  403. AP_DECLARE(int) ap_index_of_response(int status);
  404. /**
  405. * Return the Status-Line for a given status code (excluding the
  406. * HTTP-Version field). If an invalid or unknown status code is
  407. * passed, "500 Internal Server Error" will be returned.
  408. * @param status The HTTP status code
  409. * @return The Status-Line
  410. */
  411. AP_DECLARE(const char *) ap_get_status_line(int status);
  412. /* Reading a block of data from the client connection (e.g., POST arg) */
  413. /**
  414. * Setup the client to allow Apache to read the request body.
  415. * @param r The current request
  416. * @param read_policy How the server should interpret a chunked
  417. * transfer-encoding. One of: <pre>
  418. * REQUEST_NO_BODY Send 413 error if message has any body
  419. * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
  420. * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
  421. * </pre>
  422. * @return either OK or an error code
  423. */
  424. AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy);
  425. /**
  426. * Determine if the client has sent any data. This also sends a
  427. * 100 Continue response to HTTP/1.1 clients, so modules should not be called
  428. * until the module is ready to read content.
  429. * @warning Never call this function more than once.
  430. * @param r The current request
  431. * @return 0 if there is no message to read, 1 otherwise
  432. */
  433. AP_DECLARE(int) ap_should_client_block(request_rec *r);
  434. /**
  435. * Call this in a loop. It will put data into a buffer and return the length
  436. * of the input block
  437. * @param r The current request
  438. * @param buffer The buffer in which to store the data
  439. * @param bufsiz The size of the buffer
  440. * @return Number of bytes inserted into the buffer. When done reading, 0
  441. * if EOF, or -1 if there was an error
  442. */
  443. AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer, apr_size_t bufsiz);
  444. /**
  445. * Map specific APR codes returned by the filter stack to HTTP error
  446. * codes, or the default status code provided. Use it as follows:
  447. *
  448. * return ap_map_http_request_error(rv, HTTP_BAD_REQUEST);
  449. *
  450. * If the filter has already handled the error, AP_FILTER_ERROR will
  451. * be returned, which is cleanly passed through.
  452. *
  453. * These mappings imply that the filter stack is reading from the
  454. * downstream client, the proxy will map these codes differently.
  455. * @param rv APR status code
  456. * @param status Default HTTP code should the APR code not be recognised
  457. * @return Mapped HTTP status code
  458. */
  459. AP_DECLARE(int) ap_map_http_request_error(apr_status_t rv, int status);
  460. /**
  461. * In HTTP/1.1, any method can have a body. However, most GET handlers
  462. * wouldn't know what to do with a request body if they received one.
  463. * This helper routine tests for and reads any message body in the request,
  464. * simply discarding whatever it receives. We need to do this because
  465. * failing to read the request body would cause it to be interpreted
  466. * as the next request on a persistent connection.
  467. * @param r The current request
  468. * @return error status if request is malformed, OK otherwise
  469. */
  470. AP_DECLARE(int) ap_discard_request_body(request_rec *r);
  471. /**
  472. * Setup the output headers so that the client knows how to authenticate
  473. * itself the next time, if an authentication request failed.
  474. * @param r The current request
  475. */
  476. AP_DECLARE(void) ap_note_auth_failure(request_rec *r);
  477. /**
  478. * @deprecated @see ap_note_auth_failure
  479. */
  480. AP_DECLARE(void) ap_note_basic_auth_failure(request_rec *r);
  481. /**
  482. * @deprecated @see ap_note_auth_failure
  483. */
  484. AP_DECLARE(void) ap_note_digest_auth_failure(request_rec *r);
  485. /**
  486. * This hook allows modules to add support for a specific auth type to
  487. * ap_note_auth_failure
  488. * @param r the current request
  489. * @param auth_type the configured auth_type
  490. * @return OK, DECLINED
  491. */
  492. AP_DECLARE_HOOK(int, note_auth_failure, (request_rec *r, const char *auth_type))
  493. /**
  494. * Get the password from the request headers. This function has multiple side
  495. * effects due to its prior use in the old authentication framework.
  496. * ap_get_basic_auth_components() should be preferred.
  497. *
  498. * @deprecated @see ap_get_basic_auth_components
  499. * @param r The current request
  500. * @param pw The password as set in the headers
  501. * @return 0 (OK) if it set the 'pw' argument (and assured
  502. * a correct value in r->user); otherwise it returns
  503. * an error code, either HTTP_INTERNAL_SERVER_ERROR if things are
  504. * really confused, HTTP_UNAUTHORIZED if no authentication at all
  505. * seemed to be in use, or DECLINED if there was authentication but
  506. * it wasn't Basic (in which case, the caller should presumably
  507. * decline as well).
  508. */
  509. AP_DECLARE(int) ap_get_basic_auth_pw(request_rec *r, const char **pw);
  510. #define AP_GET_BASIC_AUTH_PW_NOTE "AP_GET_BASIC_AUTH_PW_NOTE"
  511. /**
  512. * Get the username and/or password from the request's Basic authentication
  513. * headers. Unlike ap_get_basic_auth_pw(), calling this function has no side
  514. * effects on the passed request_rec.
  515. *
  516. * @param r The current request
  517. * @param username If not NULL, set to the username sent by the client
  518. * @param password If not NULL, set to the password sent by the client
  519. * @return APR_SUCCESS if the credentials were successfully parsed and returned;
  520. * APR_EINVAL if there was no authentication header sent or if the
  521. * client was not using the Basic authentication scheme. username and
  522. * password are unchanged on failure.
  523. */
  524. AP_DECLARE(apr_status_t) ap_get_basic_auth_components(const request_rec *r,
  525. const char **username,
  526. const char **password);
  527. /**
  528. * parse_uri: break apart the uri
  529. * @warning Side Effects:
  530. * @li sets r->args to rest after '?' (or NULL if no '?')
  531. * @li sets r->uri to request uri (without r->args part)
  532. * @li sets r->hostname (if not set already) from request (scheme://host:port)
  533. * @param r The current request
  534. * @param uri The uri to break apart
  535. */
  536. AP_CORE_DECLARE(void) ap_parse_uri(request_rec *r, const char *uri);
  537. #define AP_GETLINE_FOLD 1 /* Whether to merge continuation lines */
  538. #define AP_GETLINE_CRLF 2 /* Whether line ends must be in the form CR LF */
  539. #define AP_GETLINE_NOSPC_EOL 4 /* Whether to consume up to and including the
  540. end of line on APR_ENOSPC */
  541. /**
  542. * Get the next line of input for the request
  543. * @param s The buffer into which to read the line
  544. * @param n The size of the buffer
  545. * @param r The request
  546. * @param flags Bit flag of multiple parsing options
  547. * AP_GETLINE_FOLD Whether to merge continuation lines
  548. * AP_GETLINE_CRLF Whether line ends must be in the form CR LF
  549. * @return The length of the line, if successful
  550. * n, if the line is too big to fit in the buffer
  551. * -1 for miscellaneous errors
  552. */
  553. AP_DECLARE(int) ap_getline(char *s, int n, request_rec *r, int flags);
  554. /**
  555. * Get the next line of input for the request
  556. *
  557. * Note: on ASCII boxes, ap_rgetline is a macro which simply calls
  558. * ap_rgetline_core to get the line of input.
  559. *
  560. * on EBCDIC boxes, ap_rgetline is a wrapper function which
  561. * translates ASCII protocol lines to the local EBCDIC code page
  562. * after getting the line of input.
  563. *
  564. * @param s Pointer to the pointer to the buffer into which the line
  565. * should be read; if *s==NULL, a buffer of the necessary size
  566. * to hold the data will be allocated from the request pool
  567. * @param n The size of the buffer
  568. * @param read The length of the line.
  569. * @param r The request
  570. * @param flags Bit flag of multiple parsing options
  571. * AP_GETLINE_FOLD Whether to merge continuation lines
  572. * AP_GETLINE_CRLF Whether line ends must be in the form CR LF
  573. * @param bb Working brigade to use when reading buckets
  574. * @return APR_SUCCESS, if successful
  575. * APR_ENOSPC, if the line is too big to fit in the buffer
  576. * Other errors where appropriate
  577. */
  578. #if APR_CHARSET_EBCDIC
  579. AP_DECLARE(apr_status_t) ap_rgetline(char **s, apr_size_t n,
  580. apr_size_t *read,
  581. request_rec *r, int flags,
  582. apr_bucket_brigade *bb);
  583. #else /* ASCII box */
  584. #define ap_rgetline(s, n, read, r, fold, bb) \
  585. ap_rgetline_core((s), (n), (read), (r), (fold), (bb))
  586. #endif
  587. /** @see ap_rgetline */
  588. AP_DECLARE(apr_status_t) ap_rgetline_core(char **s, apr_size_t n,
  589. apr_size_t *read,
  590. request_rec *r, int flags,
  591. apr_bucket_brigade *bb);
  592. /**
  593. * Get the method number associated with the given string, assumed to
  594. * contain an HTTP method. Returns M_INVALID if not recognized.
  595. * @param method A string containing a valid HTTP method
  596. * @return The method number
  597. */
  598. AP_DECLARE(int) ap_method_number_of(const char *method);
  599. /**
  600. * Get the method name associated with the given internal method
  601. * number. Returns NULL if not recognized.
  602. * @param p A pool to use for temporary allocations.
  603. * @param methnum An integer value corresponding to an internal method number
  604. * @return The name corresponding to the method number
  605. */
  606. AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum);
  607. /* Hooks */
  608. /*
  609. * pre_read_request --- run right before read_request_line(),
  610. * and not run during any subrequests.
  611. */
  612. /**
  613. * This hook allows modules to affect the request or connection immediately before
  614. * the request has been read, and before any other phases have been processes.
  615. * @param r The current request of the soon-to-be-read request
  616. * @param c The connection
  617. * @return None/void
  618. */
  619. AP_DECLARE_HOOK(void,pre_read_request,(request_rec *r, conn_rec *c))
  620. /*
  621. * post_read_request --- run right after read_request or internal_redirect,
  622. * and not run during any subrequests.
  623. */
  624. /**
  625. * This hook allows modules to affect the request immediately after the request
  626. * has been read, and before any other phases have been processes. This allows
  627. * modules to make decisions based upon the input header fields
  628. * @param r The current request
  629. * @return OK or DECLINED
  630. */
  631. AP_DECLARE_HOOK(int,post_read_request,(request_rec *r))
  632. /**
  633. * This hook allows modules to perform any module-specific logging activities
  634. * over and above the normal server things.
  635. * @param r The current request
  636. * @return OK, DECLINED, or HTTP_...
  637. */
  638. AP_DECLARE_HOOK(int,log_transaction,(request_rec *r))
  639. /**
  640. * This hook allows modules to retrieve the http scheme for a request. This
  641. * allows Apache modules to easily extend the schemes that Apache understands
  642. * @param r The current request
  643. * @return The http scheme from the request
  644. */
  645. AP_DECLARE_HOOK(const char *,http_scheme,(const request_rec *r))
  646. /**
  647. * Return the default port from the current request
  648. * @param r The current request
  649. * @return The current port
  650. */
  651. AP_DECLARE_HOOK(apr_port_t,default_port,(const request_rec *r))
  652. #define AP_PROTOCOL_HTTP1 "http/1.1"
  653. /**
  654. * Determine the list of protocols available for a connection/request. This may
  655. * be collected with or without any request sent, in which case the request is
  656. * NULL. Or it may be triggered by the request received, e.g. through the
  657. * "Upgrade" header.
  658. *
  659. * This hook will be run whenever protocols are being negotiated (ALPN as
  660. * one example). It may also be invoked at other times, e.g. when the server
  661. * wants to advertise protocols it is capable of switching to.
  662. *
  663. * The identifiers for protocols are taken from the TLS extension type ALPN:
  664. * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  665. *
  666. * If no protocols are added to the proposals, the server not perform any
  667. * switch. If the protocol selected from the proposals is the protocol
  668. * already in place, also no protocol switch will be invoked.
  669. *
  670. * The client may already have announced the protocols it is willing to
  671. * accept. These will then be listed as offers. This parameter may also
  672. * be NULL, indicating that offers from the client are not known and
  673. * the hooks should propose all protocols that are valid for the
  674. * current connection/request.
  675. *
  676. * All hooks are run, unless one returns an error. Proposals may contain
  677. * duplicates. The order in which proposals are added is usually ignored.
  678. *
  679. * @param c The current connection
  680. * @param r The current request or NULL
  681. * @param s The server/virtual host selected
  682. * @param offers A list of protocol identifiers offered by the client or
  683. * NULL to indicated that the hooks are free to propose
  684. * @param proposals The list of protocol identifiers proposed by the hooks
  685. * @return OK or DECLINED
  686. * @bug This API or implementation and order of operations should be considered
  687. * experimental and will continue to evolve in future 2.4 releases, with
  688. * a corresponding minor module magic number (MMN) bump to indicate the
  689. * API revision level.
  690. */
  691. AP_DECLARE_HOOK(int,protocol_propose,(conn_rec *c, request_rec *r,
  692. server_rec *s,
  693. const apr_array_header_t *offers,
  694. apr_array_header_t *proposals))
  695. /**
  696. * Perform a protocol switch on the connection. The exact requirements for
  697. * that depend on the protocol in place and the one switched to. The first
  698. * protocol module to handle the switch is the last module run.
  699. *
  700. * For a connection level switch (r == NULL), the handler must on return
  701. * leave the conn_rec in a state suitable for processing the switched
  702. * protocol, e.g. correct filters in place.
  703. *
  704. * For a request triggered switch (r != NULL), the protocol switch is done
  705. * before the response is sent out. When switching from "http/1.1" via Upgrade
  706. * header, the 101 intermediate response will have been sent. The
  707. * hook needs then to process the connection until it can be closed. Which
  708. * the server will enforce on hook return.
  709. * Any error the hook might encounter must already be sent by the hook itself
  710. * to the client in whatever form the new protocol requires.
  711. *
  712. * @param c The current connection
  713. * @param r The current request or NULL
  714. * @param s The server/virtual host selected
  715. * @param choices A list of protocol identifiers, normally the clients whishes
  716. * @param proposals the list of protocol identifiers proposed by the hooks
  717. * @return OK or DECLINED
  718. * @bug This API or implementation and order of operations should be considered
  719. * experimental and will continue to evolve in future 2.4 releases, with
  720. * a corresponding minor module magic number (MMN) bump to indicate the
  721. * API revision level.
  722. */
  723. AP_DECLARE_HOOK(int,protocol_switch,(conn_rec *c, request_rec *r,
  724. server_rec *s,
  725. const char *protocol))
  726. /**
  727. * Return the protocol used on the connection. Modules implementing
  728. * protocol switching must register here and return the correct protocol
  729. * identifier for connections they switched.
  730. *
  731. * To find out the protocol for the current connection, better call
  732. * @see ap_get_protocol which internally uses this hook.
  733. *
  734. * @param c The current connection
  735. * @return The identifier of the protocol in place or NULL
  736. * @bug This API or implementation and order of operations should be considered
  737. * experimental and will continue to evolve in future 2.4 releases, with
  738. * a corresponding minor module magic number (MMN) bump to indicate the
  739. * API revision level.
  740. */
  741. AP_DECLARE_HOOK(const char *,protocol_get,(const conn_rec *c))
  742. /**
  743. * Get the protocols that the connection and optional request may
  744. * upgrade to - besides the protocol currently active on the connection. These
  745. * values may be used to announce to a client what choices it has.
  746. *
  747. * If report_all == 0, only protocols more preferable than the one currently
  748. * being used, are reported. Otherwise, all available protocols beside the
  749. * current one are being reported.
  750. *
  751. * @param c The current connection
  752. * @param r The current request or NULL
  753. * @param s The server/virtual host selected or NULL
  754. * @param report_all include also protocols less preferred than the current one
  755. * @param pupgrades on return, possible protocols to upgrade to in descending order
  756. * of preference. Maybe NULL if none are available.
  757. * @bug This API or implementation and order of operations should be considered
  758. * experimental and will continue to evolve in future 2.4 releases, with
  759. * a corresponding minor module magic number (MMN) bump to indicate the
  760. * API revision level.
  761. */
  762. AP_DECLARE(apr_status_t) ap_get_protocol_upgrades(conn_rec *c, request_rec *r,
  763. server_rec *s, int report_all,
  764. const apr_array_header_t **pupgrades);
  765. /**
  766. * Select a protocol for the given connection and optional request. Will return
  767. * the protocol identifier selected which may be the protocol already in place
  768. * on the connection. The selected protocol will be NULL if non of the given
  769. * choices could be agreed upon (e.g. no proposal as made).
  770. *
  771. * A special case is where the choices itself is NULL (instead of empty). In
  772. * this case there are no restrictions imposed on protocol selection.
  773. *
  774. * @param c The current connection
  775. * @param r The current request or NULL
  776. * @param s The server/virtual host selected
  777. * @param choices A list of protocol identifiers, normally the clients whishes
  778. * @return The selected protocol or NULL if no protocol could be agreed upon
  779. * @bug This API or implementation and order of operations should be considered
  780. * experimental and will continue to evolve in future 2.4 releases, with
  781. * a corresponding minor module magic number (MMN) bump to indicate the
  782. * API revision level.
  783. */
  784. AP_DECLARE(const char *) ap_select_protocol(conn_rec *c, request_rec *r,
  785. server_rec *s,
  786. const apr_array_header_t *choices);
  787. /**
  788. * Perform the actual protocol switch. The protocol given must have been
  789. * selected before on the very same connection and request pair.
  790. *
  791. * @param c The current connection
  792. * @param r The current request or NULL
  793. * @param s The server/virtual host selected
  794. * @param protocol the protocol to switch to
  795. * @return APR_SUCCESS, if caller may continue processing as usual
  796. * APR_EOF, if caller needs to stop processing the connection
  797. * APR_EINVAL, if the protocol is already in place
  798. * APR_NOTIMPL, if no module performed the switch
  799. * Other errors where appropriate
  800. * @bug This API or implementation and order of operations should be considered
  801. * experimental and will continue to evolve in future 2.4 releases, with
  802. * a corresponding minor module magic number (MMN) bump to indicate the
  803. * API revision level.
  804. */
  805. AP_DECLARE(apr_status_t) ap_switch_protocol(conn_rec *c, request_rec *r,
  806. server_rec *s,
  807. const char *protocol);
  808. /**
  809. * Call the protocol_get hook to determine the protocol currently in use
  810. * for the given connection.
  811. *
  812. * Unless another protocol has been switch to, will default to
  813. * @see AP_PROTOCOL_HTTP1 and modules implementing a new protocol must
  814. * report a switched connection via the protocol_get hook.
  815. *
  816. * @param c The connection to determine the protocol for
  817. * @return the protocol in use, never NULL
  818. * @bug This API or implementation and order of operations should be considered
  819. * experimental and will continue to evolve in future 2.4 releases, with
  820. * a corresponding minor module magic number (MMN) bump to indicate the
  821. * API revision level.
  822. */
  823. AP_DECLARE(const char *) ap_get_protocol(conn_rec *c);
  824. /**
  825. * Check if the given protocol is an allowed choice on the given
  826. * combination of connection, request and server.
  827. *
  828. * When server is NULL, it is taken from request_rec, unless
  829. * request_rec is NULL. Then it is taken from the connection base
  830. * server.
  831. *
  832. * @param c The current connection
  833. * @param r The current request or NULL
  834. * @param s The server/virtual host selected or NULL
  835. * @param protocol the protocol to switch to
  836. * @return != 0 iff protocol is allowed
  837. * @bug This API or implementation and order of operations should be considered
  838. * experimental and will continue to evolve in future 2.4 releases, with
  839. * a corresponding minor module magic number (MMN) bump to indicate the
  840. * API revision level.
  841. */
  842. AP_DECLARE(int) ap_is_allowed_protocol(conn_rec *c, request_rec *r,
  843. server_rec *s, const char *protocol);
  844. /** @see ap_bucket_type_error */
  845. typedef struct ap_bucket_error ap_bucket_error;
  846. /**
  847. * @struct ap_bucket_error
  848. * @brief A bucket referring to an HTTP error
  849. *
  850. * This bucket can be passed down the filter stack to indicate that an
  851. * HTTP error occurred while running a filter. In order for this bucket
  852. * to be used successfully, it MUST be sent as the first bucket in the
  853. * first brigade to be sent from a given filter.
  854. */
  855. struct ap_bucket_error {
  856. /** Number of buckets using this memory */
  857. apr_bucket_refcount refcount;
  858. /** The error code */
  859. int status;
  860. /** The error string */
  861. const char *data;
  862. };
  863. /** @see ap_bucket_type_error */
  864. AP_DECLARE_DATA extern const apr_bucket_type_t ap_bucket_type_error;
  865. /**
  866. * Determine if a bucket is an error bucket
  867. * @param e The bucket to inspect
  868. * @return true or false
  869. */
  870. #define AP_BUCKET_IS_ERROR(e) (e->type == &ap_bucket_type_error)
  871. /**
  872. * Make the bucket passed in an error bucket
  873. * @param b The bucket to make into an error bucket
  874. * @param error The HTTP error code to put in the bucket.
  875. * @param buf An optional error string to put in the bucket.
  876. * @param p A pool to allocate out of.
  877. * @return The new bucket, or NULL if allocation failed
  878. */
  879. AP_DECLARE(apr_bucket *) ap_bucket_error_make(apr_bucket *b, int error,
  880. const char *buf, apr_pool_t *p);
  881. /**
  882. * Create a bucket referring to an HTTP error.
  883. * @param error The HTTP error code to put in the bucket.
  884. * @param buf An optional error string to put in the bucket.
  885. * @param p A pool to allocate the error string out of.
  886. * @param list The bucket allocator from which to allocate the bucket
  887. * @return The new bucket, or NULL if allocation failed
  888. */
  889. AP_DECLARE(apr_bucket *) ap_bucket_error_create(int error, const char *buf,
  890. apr_pool_t *p,
  891. apr_bucket_alloc_t *list);
  892. AP_DECLARE_NONSTD(apr_status_t) ap_byterange_filter(ap_filter_t *f, apr_bucket_brigade *b);
  893. AP_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f, apr_bucket_brigade *b);
  894. AP_DECLARE_NONSTD(apr_status_t) ap_content_length_filter(ap_filter_t *,
  895. apr_bucket_brigade *);
  896. AP_DECLARE_NONSTD(apr_status_t) ap_old_write_filter(ap_filter_t *f, apr_bucket_brigade *b);
  897. /**
  898. * Sett up the protocol fields for subsidiary requests
  899. * @param rnew New Sub Request
  900. * @param r current request
  901. */
  902. AP_DECLARE(void) ap_set_sub_req_protocol(request_rec *rnew, const request_rec *r);
  903. /**
  904. * A wrapup function to keep the internal accounting straight.
  905. * Indicates that there is no more content coming.
  906. * @param sub_r Subrequest that is now compete
  907. */
  908. AP_DECLARE(void) ap_finalize_sub_req_protocol(request_rec *sub_r);
  909. /**
  910. * Send an interim (HTTP 1xx) response immediately.
  911. * @param r The request
  912. * @param send_headers Whether to send&clear headers in r->headers_out
  913. */
  914. AP_DECLARE(void) ap_send_interim_response(request_rec *r, int send_headers);
  915. #ifdef __cplusplus
  916. }
  917. #endif
  918. #endif /* !APACHE_HTTP_PROTOCOL_H */
  919. /** @} */