http_protocol.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  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. * http_protocol.c --- routines which directly communicate with the client.
  18. *
  19. * Code originally by Rob McCool; much redone by Robert S. Thau
  20. * and the Apache Software Foundation.
  21. */
  22. #include "apr.h"
  23. #include "apr_strings.h"
  24. #include "apr_buckets.h"
  25. #include "apr_lib.h"
  26. #include "apr_signal.h"
  27. #define APR_WANT_STDIO /* for sscanf */
  28. #define APR_WANT_STRFUNC
  29. #define APR_WANT_MEMFUNC
  30. #include "apr_want.h"
  31. #define CORE_PRIVATE
  32. #include "util_filter.h"
  33. #include "ap_config.h"
  34. #include "httpd.h"
  35. #include "http_config.h"
  36. #include "http_core.h"
  37. #include "http_protocol.h"
  38. #include "http_main.h"
  39. #include "http_request.h"
  40. #include "http_vhost.h"
  41. #include "http_log.h" /* For errors detected in basic auth common
  42. * support code... */
  43. #include "apr_date.h" /* For apr_date_parse_http and APR_DATE_BAD */
  44. #include "util_charset.h"
  45. #include "util_ebcdic.h"
  46. #include "util_time.h"
  47. #include "ap_mpm.h"
  48. #include "mod_core.h"
  49. #if APR_HAVE_STDARG_H
  50. #include <stdarg.h>
  51. #endif
  52. #if APR_HAVE_UNISTD_H
  53. #include <unistd.h>
  54. #endif
  55. /* New Apache routine to map status codes into array indicies
  56. * e.g. 100 -> 0, 101 -> 1, 200 -> 2 ...
  57. * The number of status lines must equal the value of RESPONSE_CODES (httpd.h)
  58. * and must be listed in order.
  59. */
  60. #ifdef UTS21
  61. /* The second const triggers an assembler bug on UTS 2.1.
  62. * Another workaround is to move some code out of this file into another,
  63. * but this is easier. Dave Dykstra, 3/31/99
  64. */
  65. static const char * status_lines[RESPONSE_CODES] =
  66. #else
  67. static const char * const status_lines[RESPONSE_CODES] =
  68. #endif
  69. {
  70. "100 Continue",
  71. "101 Switching Protocols",
  72. "102 Processing",
  73. #define LEVEL_200 3
  74. "200 OK",
  75. "201 Created",
  76. "202 Accepted",
  77. "203 Non-Authoritative Information",
  78. "204 No Content",
  79. "205 Reset Content",
  80. "206 Partial Content",
  81. "207 Multi-Status",
  82. #define LEVEL_300 11
  83. "300 Multiple Choices",
  84. "301 Moved Permanently",
  85. "302 Found",
  86. "303 See Other",
  87. "304 Not Modified",
  88. "305 Use Proxy",
  89. "306 unused",
  90. "307 Temporary Redirect",
  91. #define LEVEL_400 19
  92. "400 Bad Request",
  93. "401 Authorization Required",
  94. "402 Payment Required",
  95. "403 Forbidden",
  96. "404 Not Found",
  97. "405 Method Not Allowed",
  98. "406 Not Acceptable",
  99. "407 Proxy Authentication Required",
  100. "408 Request Time-out",
  101. "409 Conflict",
  102. "410 Gone",
  103. "411 Length Required",
  104. "412 Precondition Failed",
  105. "413 Request Entity Too Large",
  106. "414 Request-URI Too Large",
  107. "415 Unsupported Media Type",
  108. "416 Requested Range Not Satisfiable",
  109. "417 Expectation Failed",
  110. "418 unused",
  111. "419 unused",
  112. "420 unused",
  113. "421 unused",
  114. "422 Unprocessable Entity",
  115. "423 Locked",
  116. "424 Failed Dependency",
  117. /* This is a hack, but it is required for ap_index_of_response
  118. * to work with 426.
  119. */
  120. "425 No code",
  121. "426 Upgrade Required",
  122. #define LEVEL_500 46
  123. "500 Internal Server Error",
  124. "501 Method Not Implemented",
  125. "502 Bad Gateway",
  126. "503 Service Temporarily Unavailable",
  127. "504 Gateway Time-out",
  128. "505 HTTP Version Not Supported",
  129. "506 Variant Also Negotiates",
  130. "507 Insufficient Storage",
  131. "508 unused",
  132. "509 unused",
  133. "510 Not Extended"
  134. };
  135. APR_HOOK_STRUCT(
  136. APR_HOOK_LINK(insert_error_filter)
  137. )
  138. AP_IMPLEMENT_HOOK_VOID(insert_error_filter, (request_rec *r), (r))
  139. /* The index of the first bit field that is used to index into a limit
  140. * bitmask. M_INVALID + 1 to METHOD_NUMBER_LAST.
  141. */
  142. #define METHOD_NUMBER_FIRST (M_INVALID + 1)
  143. /* The max method number. Method numbers are used to shift bitmasks,
  144. * so this cannot exceed 63, and all bits high is equal to -1, which is a
  145. * special flag, so the last bit used has index 62.
  146. */
  147. #define METHOD_NUMBER_LAST 62
  148. AP_DECLARE(int) ap_set_keepalive(request_rec *r)
  149. {
  150. int ka_sent = 0;
  151. int left = r->server->keep_alive_max - r->connection->keepalives;
  152. int wimpy = ap_find_token(r->pool,
  153. apr_table_get(r->headers_out, "Connection"),
  154. "close");
  155. const char *conn = apr_table_get(r->headers_in, "Connection");
  156. /* The following convoluted conditional determines whether or not
  157. * the current connection should remain persistent after this response
  158. * (a.k.a. HTTP Keep-Alive) and whether or not the output message
  159. * body should use the HTTP/1.1 chunked transfer-coding. In English,
  160. *
  161. * IF we have not marked this connection as errored;
  162. * and the client isn't expecting 100-continue (PR47087 - more
  163. * input here could be the client continuing when we're
  164. * closing the request).
  165. * and the response body has a defined length due to the status code
  166. * being 304 or 204, the request method being HEAD, already
  167. * having defined Content-Length or Transfer-Encoding: chunked, or
  168. * the request version being HTTP/1.1 and thus capable of being set
  169. * as chunked [we know the (r->chunked = 1) side-effect is ugly];
  170. * and the server configuration enables keep-alive;
  171. * and the server configuration has a reasonable inter-request timeout;
  172. * and there is no maximum # requests or the max hasn't been reached;
  173. * and the response status does not require a close;
  174. * and the response generator has not already indicated close;
  175. * and the client did not request non-persistence (Connection: close);
  176. * and we haven't been configured to ignore the buggy twit
  177. * or they're a buggy twit coming through a HTTP/1.1 proxy
  178. * and the client is requesting an HTTP/1.0-style keep-alive
  179. * or the client claims to be HTTP/1.1 compliant (perhaps a proxy);
  180. * and this MPM process is not already exiting
  181. * THEN we can be persistent, which requires more headers be output.
  182. *
  183. * Note that the condition evaluation order is extremely important.
  184. */
  185. if ((r->connection->keepalive != AP_CONN_CLOSE)
  186. && !r->expecting_100
  187. && ((r->status == HTTP_NOT_MODIFIED)
  188. || (r->status == HTTP_NO_CONTENT)
  189. || r->header_only
  190. || apr_table_get(r->headers_out, "Content-Length")
  191. || ap_find_last_token(r->pool,
  192. apr_table_get(r->headers_out,
  193. "Transfer-Encoding"),
  194. "chunked")
  195. || ((r->proto_num >= HTTP_VERSION(1,1))
  196. && (r->chunked = 1))) /* THIS CODE IS CORRECT, see above. */
  197. && r->server->keep_alive
  198. && (r->server->keep_alive_timeout > 0)
  199. && ((r->server->keep_alive_max == 0)
  200. || (left > 0))
  201. && !ap_status_drops_connection(r->status)
  202. && !wimpy
  203. && !ap_find_token(r->pool, conn, "close")
  204. && (!apr_table_get(r->subprocess_env, "nokeepalive")
  205. || apr_table_get(r->headers_in, "Via"))
  206. && ((ka_sent = ap_find_token(r->pool, conn, "keep-alive"))
  207. || (r->proto_num >= HTTP_VERSION(1,1)))
  208. && !ap_graceful_stop_signalled()) {
  209. r->connection->keepalive = AP_CONN_KEEPALIVE;
  210. r->connection->keepalives++;
  211. /* If they sent a Keep-Alive token, send one back */
  212. if (ka_sent) {
  213. if (r->server->keep_alive_max) {
  214. apr_table_setn(r->headers_out, "Keep-Alive",
  215. apr_psprintf(r->pool, "timeout=%d, max=%d",
  216. (int)apr_time_sec(r->server->keep_alive_timeout),
  217. left));
  218. }
  219. else {
  220. apr_table_setn(r->headers_out, "Keep-Alive",
  221. apr_psprintf(r->pool, "timeout=%d",
  222. (int)apr_time_sec(r->server->keep_alive_timeout)));
  223. }
  224. apr_table_mergen(r->headers_out, "Connection", "Keep-Alive");
  225. }
  226. return 1;
  227. }
  228. /* Otherwise, we need to indicate that we will be closing this
  229. * connection immediately after the current response.
  230. *
  231. * We only really need to send "close" to HTTP/1.1 clients, but we
  232. * always send it anyway, because a broken proxy may identify itself
  233. * as HTTP/1.0, but pass our request along with our HTTP/1.1 tag
  234. * to a HTTP/1.1 client. Better safe than sorry.
  235. */
  236. if (!wimpy) {
  237. apr_table_mergen(r->headers_out, "Connection", "close");
  238. }
  239. /*
  240. * If we had previously been a keepalive connection and this
  241. * is the last one, then bump up the number of keepalives
  242. * we've had
  243. */
  244. if ((r->connection->keepalive != AP_CONN_CLOSE)
  245. && r->server->keep_alive_max
  246. && !left) {
  247. r->connection->keepalives++;
  248. }
  249. r->connection->keepalive = AP_CONN_CLOSE;
  250. return 0;
  251. }
  252. AP_DECLARE(int) ap_meets_conditions(request_rec *r)
  253. {
  254. const char *etag;
  255. const char *if_match, *if_modified_since, *if_unmodified, *if_nonematch;
  256. apr_time_t tmp_time;
  257. apr_int64_t mtime;
  258. int not_modified = 0;
  259. /* Check for conditional requests --- note that we only want to do
  260. * this if we are successful so far and we are not processing a
  261. * subrequest or an ErrorDocument.
  262. *
  263. * The order of the checks is important, since ETag checks are supposed
  264. * to be more accurate than checks relative to the modification time.
  265. * However, not all documents are guaranteed to *have* ETags, and some
  266. * might have Last-Modified values w/o ETags, so this gets a little
  267. * complicated.
  268. */
  269. if (!ap_is_HTTP_SUCCESS(r->status) || r->no_local_copy) {
  270. return OK;
  271. }
  272. etag = apr_table_get(r->headers_out, "ETag");
  273. /* All of our comparisons must be in seconds, because that's the
  274. * highest time resolution the HTTP specification allows.
  275. */
  276. /* XXX: we should define a "time unset" constant */
  277. tmp_time = ((r->mtime != 0) ? r->mtime : apr_time_now());
  278. mtime = apr_time_sec(tmp_time);
  279. /* If an If-Match request-header field was given
  280. * AND the field value is not "*" (meaning match anything)
  281. * AND if our strong ETag does not match any entity tag in that field,
  282. * respond with a status of 412 (Precondition Failed).
  283. */
  284. if ((if_match = apr_table_get(r->headers_in, "If-Match")) != NULL) {
  285. if (if_match[0] != '*'
  286. && (etag == NULL || etag[0] == 'W'
  287. || !ap_find_list_item(r->pool, if_match, etag))) {
  288. return HTTP_PRECONDITION_FAILED;
  289. }
  290. }
  291. else {
  292. /* Else if a valid If-Unmodified-Since request-header field was given
  293. * AND the requested resource has been modified since the time
  294. * specified in this field, then the server MUST
  295. * respond with a status of 412 (Precondition Failed).
  296. */
  297. if_unmodified = apr_table_get(r->headers_in, "If-Unmodified-Since");
  298. if (if_unmodified != NULL) {
  299. apr_time_t ius = apr_date_parse_http(if_unmodified);
  300. if ((ius != APR_DATE_BAD) && (mtime > apr_time_sec(ius))) {
  301. return HTTP_PRECONDITION_FAILED;
  302. }
  303. }
  304. }
  305. /* If an If-None-Match request-header field was given
  306. * AND the field value is "*" (meaning match anything)
  307. * OR our ETag matches any of the entity tags in that field, fail.
  308. *
  309. * If the request method was GET or HEAD, failure means the server
  310. * SHOULD respond with a 304 (Not Modified) response.
  311. * For all other request methods, failure means the server MUST
  312. * respond with a status of 412 (Precondition Failed).
  313. *
  314. * GET or HEAD allow weak etag comparison, all other methods require
  315. * strong comparison. We can only use weak if it's not a range request.
  316. */
  317. if_nonematch = apr_table_get(r->headers_in, "If-None-Match");
  318. if (if_nonematch != NULL) {
  319. if (r->method_number == M_GET) {
  320. if (if_nonematch[0] == '*') {
  321. not_modified = 1;
  322. }
  323. else if (etag != NULL) {
  324. if (apr_table_get(r->headers_in, "Range")) {
  325. not_modified = etag[0] != 'W'
  326. && ap_find_list_item(r->pool,
  327. if_nonematch, etag);
  328. }
  329. else {
  330. not_modified = ap_find_list_item(r->pool,
  331. if_nonematch, etag);
  332. }
  333. }
  334. }
  335. else if (if_nonematch[0] == '*'
  336. || (etag != NULL
  337. && ap_find_list_item(r->pool, if_nonematch, etag))) {
  338. return HTTP_PRECONDITION_FAILED;
  339. }
  340. }
  341. /* If a valid If-Modified-Since request-header field was given
  342. * AND it is a GET or HEAD request
  343. * AND the requested resource has not been modified since the time
  344. * specified in this field, then the server MUST
  345. * respond with a status of 304 (Not Modified).
  346. * A date later than the server's current request time is invalid.
  347. */
  348. if (r->method_number == M_GET
  349. && (not_modified || !if_nonematch)
  350. && (if_modified_since =
  351. apr_table_get(r->headers_in,
  352. "If-Modified-Since")) != NULL) {
  353. apr_time_t ims_time;
  354. apr_int64_t ims, reqtime;
  355. ims_time = apr_date_parse_http(if_modified_since);
  356. ims = apr_time_sec(ims_time);
  357. reqtime = apr_time_sec(r->request_time);
  358. not_modified = ims >= mtime && ims <= reqtime;
  359. }
  360. if (not_modified) {
  361. return HTTP_NOT_MODIFIED;
  362. }
  363. return OK;
  364. }
  365. /**
  366. * Singleton registry of additional methods. This maps new method names
  367. * such as "MYGET" to methnums, which are int offsets into bitmasks.
  368. *
  369. * This follows the same technique as standard M_GET, M_POST, etc. These
  370. * are dynamically assigned when modules are loaded and <Limit GET MYGET>
  371. * directives are processed.
  372. */
  373. static apr_hash_t *methods_registry = NULL;
  374. static int cur_method_number = METHOD_NUMBER_FIRST;
  375. /* internal function to register one method/number pair */
  376. static void register_one_method(apr_pool_t *p, const char *methname,
  377. int methnum)
  378. {
  379. int *pnum = apr_palloc(p, sizeof(*pnum));
  380. *pnum = methnum;
  381. apr_hash_set(methods_registry, methname, APR_HASH_KEY_STRING, pnum);
  382. }
  383. /* This internal function is used to clear the method registry
  384. * and reset the cur_method_number counter.
  385. */
  386. static apr_status_t ap_method_registry_destroy(void *notused)
  387. {
  388. methods_registry = NULL;
  389. cur_method_number = METHOD_NUMBER_FIRST;
  390. return APR_SUCCESS;
  391. }
  392. AP_DECLARE(void) ap_method_registry_init(apr_pool_t *p)
  393. {
  394. methods_registry = apr_hash_make(p);
  395. apr_pool_cleanup_register(p, NULL,
  396. ap_method_registry_destroy,
  397. apr_pool_cleanup_null);
  398. /* put all the standard methods into the registry hash to ease the
  399. mapping operations between name and number */
  400. register_one_method(p, "GET", M_GET);
  401. register_one_method(p, "PUT", M_PUT);
  402. register_one_method(p, "POST", M_POST);
  403. register_one_method(p, "DELETE", M_DELETE);
  404. register_one_method(p, "CONNECT", M_CONNECT);
  405. register_one_method(p, "OPTIONS", M_OPTIONS);
  406. register_one_method(p, "TRACE", M_TRACE);
  407. register_one_method(p, "PATCH", M_PATCH);
  408. register_one_method(p, "PROPFIND", M_PROPFIND);
  409. register_one_method(p, "PROPPATCH", M_PROPPATCH);
  410. register_one_method(p, "MKCOL", M_MKCOL);
  411. register_one_method(p, "COPY", M_COPY);
  412. register_one_method(p, "MOVE", M_MOVE);
  413. register_one_method(p, "LOCK", M_LOCK);
  414. register_one_method(p, "UNLOCK", M_UNLOCK);
  415. register_one_method(p, "VERSION-CONTROL", M_VERSION_CONTROL);
  416. register_one_method(p, "CHECKOUT", M_CHECKOUT);
  417. register_one_method(p, "UNCHECKOUT", M_UNCHECKOUT);
  418. register_one_method(p, "CHECKIN", M_CHECKIN);
  419. register_one_method(p, "UPDATE", M_UPDATE);
  420. register_one_method(p, "LABEL", M_LABEL);
  421. register_one_method(p, "REPORT", M_REPORT);
  422. register_one_method(p, "MKWORKSPACE", M_MKWORKSPACE);
  423. register_one_method(p, "MKACTIVITY", M_MKACTIVITY);
  424. register_one_method(p, "BASELINE-CONTROL", M_BASELINE_CONTROL);
  425. register_one_method(p, "MERGE", M_MERGE);
  426. }
  427. AP_DECLARE(int) ap_method_register(apr_pool_t *p, const char *methname)
  428. {
  429. int *methnum;
  430. if (methods_registry == NULL) {
  431. ap_method_registry_init(p);
  432. }
  433. if (methname == NULL) {
  434. return M_INVALID;
  435. }
  436. /* Check if the method was previously registered. If it was
  437. * return the associated method number.
  438. */
  439. methnum = (int *)apr_hash_get(methods_registry, methname,
  440. APR_HASH_KEY_STRING);
  441. if (methnum != NULL)
  442. return *methnum;
  443. if (cur_method_number > METHOD_NUMBER_LAST) {
  444. /* The method registry has run out of dynamically
  445. * assignable method numbers. Log this and return M_INVALID.
  446. */
  447. ap_log_perror(APLOG_MARK, APLOG_ERR, 0, p,
  448. "Maximum new request methods %d reached while "
  449. "registering method %s.",
  450. METHOD_NUMBER_LAST, methname);
  451. return M_INVALID;
  452. }
  453. register_one_method(p, methname, cur_method_number);
  454. return cur_method_number++;
  455. }
  456. #define UNKNOWN_METHOD (-1)
  457. static int lookup_builtin_method(const char *method, apr_size_t len)
  458. {
  459. /* Note: the following code was generated by the "shilka" tool from
  460. the "cocom" parsing/compilation toolkit. It is an optimized lookup
  461. based on analysis of the input keywords. Postprocessing was done
  462. on the shilka output, but the basic structure and analysis is
  463. from there. Should new HTTP methods be added, then manual insertion
  464. into this code is fine, or simply re-running the shilka tool on
  465. the appropriate input. */
  466. /* Note: it is also quite reasonable to just use our method_registry,
  467. but I'm assuming (probably incorrectly) we want more speed here
  468. (based on the optimizations the previous code was doing). */
  469. switch (len)
  470. {
  471. case 3:
  472. switch (method[0])
  473. {
  474. case 'P':
  475. return (method[1] == 'U'
  476. && method[2] == 'T'
  477. ? M_PUT : UNKNOWN_METHOD);
  478. case 'G':
  479. return (method[1] == 'E'
  480. && method[2] == 'T'
  481. ? M_GET : UNKNOWN_METHOD);
  482. default:
  483. return UNKNOWN_METHOD;
  484. }
  485. case 4:
  486. switch (method[0])
  487. {
  488. case 'H':
  489. return (method[1] == 'E'
  490. && method[2] == 'A'
  491. && method[3] == 'D'
  492. ? M_GET : UNKNOWN_METHOD);
  493. case 'P':
  494. return (method[1] == 'O'
  495. && method[2] == 'S'
  496. && method[3] == 'T'
  497. ? M_POST : UNKNOWN_METHOD);
  498. case 'M':
  499. return (method[1] == 'O'
  500. && method[2] == 'V'
  501. && method[3] == 'E'
  502. ? M_MOVE : UNKNOWN_METHOD);
  503. case 'L':
  504. return (method[1] == 'O'
  505. && method[2] == 'C'
  506. && method[3] == 'K'
  507. ? M_LOCK : UNKNOWN_METHOD);
  508. case 'C':
  509. return (method[1] == 'O'
  510. && method[2] == 'P'
  511. && method[3] == 'Y'
  512. ? M_COPY : UNKNOWN_METHOD);
  513. default:
  514. return UNKNOWN_METHOD;
  515. }
  516. case 5:
  517. switch (method[2])
  518. {
  519. case 'T':
  520. return (memcmp(method, "PATCH", 5) == 0
  521. ? M_PATCH : UNKNOWN_METHOD);
  522. case 'R':
  523. return (memcmp(method, "MERGE", 5) == 0
  524. ? M_MERGE : UNKNOWN_METHOD);
  525. case 'C':
  526. return (memcmp(method, "MKCOL", 5) == 0
  527. ? M_MKCOL : UNKNOWN_METHOD);
  528. case 'B':
  529. return (memcmp(method, "LABEL", 5) == 0
  530. ? M_LABEL : UNKNOWN_METHOD);
  531. case 'A':
  532. return (memcmp(method, "TRACE", 5) == 0
  533. ? M_TRACE : UNKNOWN_METHOD);
  534. default:
  535. return UNKNOWN_METHOD;
  536. }
  537. case 6:
  538. switch (method[0])
  539. {
  540. case 'U':
  541. switch (method[5])
  542. {
  543. case 'K':
  544. return (memcmp(method, "UNLOCK", 6) == 0
  545. ? M_UNLOCK : UNKNOWN_METHOD);
  546. case 'E':
  547. return (memcmp(method, "UPDATE", 6) == 0
  548. ? M_UPDATE : UNKNOWN_METHOD);
  549. default:
  550. return UNKNOWN_METHOD;
  551. }
  552. case 'R':
  553. return (memcmp(method, "REPORT", 6) == 0
  554. ? M_REPORT : UNKNOWN_METHOD);
  555. case 'D':
  556. return (memcmp(method, "DELETE", 6) == 0
  557. ? M_DELETE : UNKNOWN_METHOD);
  558. default:
  559. return UNKNOWN_METHOD;
  560. }
  561. case 7:
  562. switch (method[1])
  563. {
  564. case 'P':
  565. return (memcmp(method, "OPTIONS", 7) == 0
  566. ? M_OPTIONS : UNKNOWN_METHOD);
  567. case 'O':
  568. return (memcmp(method, "CONNECT", 7) == 0
  569. ? M_CONNECT : UNKNOWN_METHOD);
  570. case 'H':
  571. return (memcmp(method, "CHECKIN", 7) == 0
  572. ? M_CHECKIN : UNKNOWN_METHOD);
  573. default:
  574. return UNKNOWN_METHOD;
  575. }
  576. case 8:
  577. switch (method[0])
  578. {
  579. case 'P':
  580. return (memcmp(method, "PROPFIND", 8) == 0
  581. ? M_PROPFIND : UNKNOWN_METHOD);
  582. case 'C':
  583. return (memcmp(method, "CHECKOUT", 8) == 0
  584. ? M_CHECKOUT : UNKNOWN_METHOD);
  585. default:
  586. return UNKNOWN_METHOD;
  587. }
  588. case 9:
  589. return (memcmp(method, "PROPPATCH", 9) == 0
  590. ? M_PROPPATCH : UNKNOWN_METHOD);
  591. case 10:
  592. switch (method[0])
  593. {
  594. case 'U':
  595. return (memcmp(method, "UNCHECKOUT", 10) == 0
  596. ? M_UNCHECKOUT : UNKNOWN_METHOD);
  597. case 'M':
  598. return (memcmp(method, "MKACTIVITY", 10) == 0
  599. ? M_MKACTIVITY : UNKNOWN_METHOD);
  600. default:
  601. return UNKNOWN_METHOD;
  602. }
  603. case 11:
  604. return (memcmp(method, "MKWORKSPACE", 11) == 0
  605. ? M_MKWORKSPACE : UNKNOWN_METHOD);
  606. case 15:
  607. return (memcmp(method, "VERSION-CONTROL", 15) == 0
  608. ? M_VERSION_CONTROL : UNKNOWN_METHOD);
  609. case 16:
  610. return (memcmp(method, "BASELINE-CONTROL", 16) == 0
  611. ? M_BASELINE_CONTROL : UNKNOWN_METHOD);
  612. default:
  613. return UNKNOWN_METHOD;
  614. }
  615. /* NOTREACHED */
  616. }
  617. /* Get the method number associated with the given string, assumed to
  618. * contain an HTTP method. Returns M_INVALID if not recognized.
  619. *
  620. * This is the first step toward placing method names in a configurable
  621. * list. Hopefully it (and other routines) can eventually be moved to
  622. * something like a mod_http_methods.c, complete with config stuff.
  623. */
  624. AP_DECLARE(int) ap_method_number_of(const char *method)
  625. {
  626. int len = strlen(method);
  627. int which = lookup_builtin_method(method, len);
  628. if (which != UNKNOWN_METHOD)
  629. return which;
  630. /* check if the method has been dynamically registered */
  631. if (methods_registry != NULL) {
  632. int *methnum = apr_hash_get(methods_registry, method, len);
  633. if (methnum != NULL) {
  634. return *methnum;
  635. }
  636. }
  637. return M_INVALID;
  638. }
  639. /*
  640. * Turn a known method number into a name.
  641. */
  642. AP_DECLARE(const char *) ap_method_name_of(apr_pool_t *p, int methnum)
  643. {
  644. apr_hash_index_t *hi = apr_hash_first(p, methods_registry);
  645. /* scan through the hash table, looking for a value that matches
  646. the provided method number. */
  647. for (; hi; hi = apr_hash_next(hi)) {
  648. const void *key;
  649. void *val;
  650. apr_hash_this(hi, &key, NULL, &val);
  651. if (*(int *)val == methnum)
  652. return key;
  653. }
  654. /* it wasn't found in the hash */
  655. return NULL;
  656. }
  657. /* The index is found by its offset from the x00 code of each level.
  658. * Although this is fast, it will need to be replaced if some nutcase
  659. * decides to define a high-numbered code before the lower numbers.
  660. * If that sad event occurs, replace the code below with a linear search
  661. * from status_lines[shortcut[i]] to status_lines[shortcut[i+1]-1];
  662. */
  663. AP_DECLARE(int) ap_index_of_response(int status)
  664. {
  665. static int shortcut[6] = {0, LEVEL_200, LEVEL_300, LEVEL_400,
  666. LEVEL_500, RESPONSE_CODES};
  667. int i, pos;
  668. if (status < 100) { /* Below 100 is illegal for HTTP status */
  669. return LEVEL_500;
  670. }
  671. for (i = 0; i < 5; i++) {
  672. status -= 100;
  673. if (status < 100) {
  674. pos = (status + shortcut[i]);
  675. if (pos < shortcut[i + 1]) {
  676. return pos;
  677. }
  678. else {
  679. return LEVEL_500; /* status unknown (falls in gap) */
  680. }
  681. }
  682. }
  683. return LEVEL_500; /* 600 or above is also illegal */
  684. }
  685. AP_DECLARE(const char *) ap_get_status_line(int status)
  686. {
  687. return status_lines[ap_index_of_response(status)];
  688. }
  689. /* Build the Allow field-value from the request handler method mask.
  690. * Note that we always allow TRACE, since it is handled below.
  691. */
  692. static char *make_allow(request_rec *r)
  693. {
  694. char *list;
  695. apr_int64_t mask;
  696. apr_array_header_t *allow = apr_array_make(r->pool, 10, sizeof(char *));
  697. apr_hash_index_t *hi = apr_hash_first(r->pool, methods_registry);
  698. /* For TRACE below */
  699. core_server_config *conf =
  700. ap_get_module_config(r->server->module_config, &core_module);
  701. mask = r->allowed_methods->method_mask;
  702. for (; hi; hi = apr_hash_next(hi)) {
  703. const void *key;
  704. void *val;
  705. apr_hash_this(hi, &key, NULL, &val);
  706. if ((mask & (AP_METHOD_BIT << *(int *)val)) != 0) {
  707. *(const char **)apr_array_push(allow) = key;
  708. /* the M_GET method actually refers to two methods */
  709. if (*(int *)val == M_GET)
  710. *(const char **)apr_array_push(allow) = "HEAD";
  711. }
  712. }
  713. /* TRACE is tested on a per-server basis */
  714. if (conf->trace_enable != AP_TRACE_DISABLE)
  715. *(const char **)apr_array_push(allow) = "TRACE";
  716. list = apr_array_pstrcat(r->pool, allow, ',');
  717. /* ### this is rather annoying. we should enforce registration of
  718. ### these methods */
  719. if ((mask & (AP_METHOD_BIT << M_INVALID))
  720. && (r->allowed_methods->method_list != NULL)
  721. && (r->allowed_methods->method_list->nelts != 0)) {
  722. int i;
  723. char **xmethod = (char **) r->allowed_methods->method_list->elts;
  724. /*
  725. * Append all of the elements of r->allowed_methods->method_list
  726. */
  727. for (i = 0; i < r->allowed_methods->method_list->nelts; ++i) {
  728. list = apr_pstrcat(r->pool, list, ",", xmethod[i], NULL);
  729. }
  730. }
  731. return list;
  732. }
  733. AP_DECLARE(int) ap_send_http_options(request_rec *r)
  734. {
  735. if (r->assbackwards) {
  736. return DECLINED;
  737. }
  738. apr_table_setn(r->headers_out, "Allow", make_allow(r));
  739. /* the request finalization will send an EOS, which will flush all
  740. * the headers out (including the Allow header)
  741. */
  742. return OK;
  743. }
  744. AP_DECLARE(void) ap_set_content_type(request_rec *r, const char *ct)
  745. {
  746. if (!ct) {
  747. r->content_type = NULL;
  748. }
  749. else if (!r->content_type || strcmp(r->content_type, ct)) {
  750. r->content_type = ct;
  751. /* Insert filters requested by the AddOutputFiltersByType
  752. * configuration directive. Content-type filters must be
  753. * inserted after the content handlers have run because
  754. * only then, do we reliably know the content-type.
  755. */
  756. ap_add_output_filters_by_type(r);
  757. }
  758. }
  759. static const char *add_optional_notes(request_rec *r,
  760. const char *prefix,
  761. const char *key,
  762. const char *suffix)
  763. {
  764. const char *notes, *result;
  765. if ((notes = apr_table_get(r->notes, key)) == NULL) {
  766. result = apr_pstrcat(r->pool, prefix, suffix, NULL);
  767. }
  768. else {
  769. result = apr_pstrcat(r->pool, prefix, notes, suffix, NULL);
  770. }
  771. return result;
  772. }
  773. /* construct and return the default error message for a given
  774. * HTTP defined error code
  775. */
  776. static const char *get_canned_error_string(int status,
  777. request_rec *r,
  778. const char *location)
  779. {
  780. apr_pool_t *p = r->pool;
  781. const char *error_notes, *h1, *s1;
  782. switch (status) {
  783. case HTTP_MOVED_PERMANENTLY:
  784. case HTTP_MOVED_TEMPORARILY:
  785. case HTTP_TEMPORARY_REDIRECT:
  786. return(apr_pstrcat(p,
  787. "<p>The document has moved <a href=\"",
  788. ap_escape_html(r->pool, location),
  789. "\">here</a>.</p>\n",
  790. NULL));
  791. case HTTP_SEE_OTHER:
  792. return(apr_pstrcat(p,
  793. "<p>The answer to your request is located "
  794. "<a href=\"",
  795. ap_escape_html(r->pool, location),
  796. "\">here</a>.</p>\n",
  797. NULL));
  798. case HTTP_USE_PROXY:
  799. return(apr_pstrcat(p,
  800. "<p>This resource is only accessible "
  801. "through the proxy\n",
  802. ap_escape_html(r->pool, location),
  803. "<br />\nYou will need to configure "
  804. "your client to use that proxy.</p>\n",
  805. NULL));
  806. case HTTP_PROXY_AUTHENTICATION_REQUIRED:
  807. case HTTP_UNAUTHORIZED:
  808. return("<p>This server could not verify that you\n"
  809. "are authorized to access the document\n"
  810. "requested. Either you supplied the wrong\n"
  811. "credentials (e.g., bad password), or your\n"
  812. "browser doesn't understand how to supply\n"
  813. "the credentials required.</p>\n");
  814. case HTTP_BAD_REQUEST:
  815. return(add_optional_notes(r,
  816. "<p>Your browser sent a request that "
  817. "this server could not understand.<br />\n",
  818. "error-notes",
  819. "</p>\n"));
  820. case HTTP_FORBIDDEN:
  821. return(apr_pstrcat(p,
  822. "<p>You don't have permission to access ",
  823. ap_escape_html(r->pool, r->uri),
  824. "\non this server.</p>\n",
  825. NULL));
  826. case HTTP_NOT_FOUND:
  827. return(apr_pstrcat(p,
  828. "<p>The requested URL ",
  829. ap_escape_html(r->pool, r->uri),
  830. " was not found on this server.</p>\n",
  831. NULL));
  832. case HTTP_METHOD_NOT_ALLOWED:
  833. return(apr_pstrcat(p,
  834. "<p>The requested method ",
  835. ap_escape_html(r->pool, r->method),
  836. " is not allowed for the URL ",
  837. ap_escape_html(r->pool, r->uri),
  838. ".</p>\n",
  839. NULL));
  840. case HTTP_NOT_ACCEPTABLE:
  841. s1 = apr_pstrcat(p,
  842. "<p>An appropriate representation of the "
  843. "requested resource ",
  844. ap_escape_html(r->pool, r->uri),
  845. " could not be found on this server.</p>\n",
  846. NULL);
  847. return(add_optional_notes(r, s1, "variant-list", ""));
  848. case HTTP_MULTIPLE_CHOICES:
  849. return(add_optional_notes(r, "", "variant-list", ""));
  850. case HTTP_LENGTH_REQUIRED:
  851. s1 = apr_pstrcat(p,
  852. "<p>A request of the requested method ",
  853. ap_escape_html(r->pool, r->method),
  854. " requires a valid Content-length.<br />\n",
  855. NULL);
  856. return(add_optional_notes(r, s1, "error-notes", "</p>\n"));
  857. case HTTP_PRECONDITION_FAILED:
  858. return(apr_pstrcat(p,
  859. "<p>The precondition on the request "
  860. "for the URL ",
  861. ap_escape_html(r->pool, r->uri),
  862. " evaluated to false.</p>\n",
  863. NULL));
  864. case HTTP_NOT_IMPLEMENTED:
  865. s1 = apr_pstrcat(p,
  866. "<p>",
  867. ap_escape_html(r->pool, r->method), " to ",
  868. ap_escape_html(r->pool, r->uri),
  869. " not supported.<br />\n",
  870. NULL);
  871. return(add_optional_notes(r, s1, "error-notes", "</p>\n"));
  872. case HTTP_BAD_GATEWAY:
  873. s1 = "<p>The proxy server received an invalid" CRLF
  874. "response from an upstream server.<br />" CRLF;
  875. return(add_optional_notes(r, s1, "error-notes", "</p>\n"));
  876. case HTTP_VARIANT_ALSO_VARIES:
  877. return(apr_pstrcat(p,
  878. "<p>A variant for the requested "
  879. "resource\n<pre>\n",
  880. ap_escape_html(r->pool, r->uri),
  881. "\n</pre>\nis itself a negotiable resource. "
  882. "This indicates a configuration error.</p>\n",
  883. NULL));
  884. case HTTP_REQUEST_TIME_OUT:
  885. return("<p>Server timeout waiting for the HTTP request from the client.</p>\n");
  886. case HTTP_GONE:
  887. return(apr_pstrcat(p,
  888. "<p>The requested resource<br />",
  889. ap_escape_html(r->pool, r->uri),
  890. "<br />\nis no longer available on this server "
  891. "and there is no forwarding address.\n"
  892. "Please remove all references to this "
  893. "resource.</p>\n",
  894. NULL));
  895. case HTTP_REQUEST_ENTITY_TOO_LARGE:
  896. return(apr_pstrcat(p,
  897. "The requested resource<br />",
  898. ap_escape_html(r->pool, r->uri), "<br />\n",
  899. "does not allow request data with ",
  900. ap_escape_html(r->pool, r->method),
  901. " requests, or the amount of data provided in\n"
  902. "the request exceeds the capacity limit.\n",
  903. NULL));
  904. case HTTP_REQUEST_URI_TOO_LARGE:
  905. s1 = "<p>The requested URL's length exceeds the capacity\n"
  906. "limit for this server.<br />\n";
  907. return(add_optional_notes(r, s1, "error-notes", "</p>\n"));
  908. case HTTP_UNSUPPORTED_MEDIA_TYPE:
  909. return("<p>The supplied request data is not in a format\n"
  910. "acceptable for processing by this resource.</p>\n");
  911. case HTTP_RANGE_NOT_SATISFIABLE:
  912. return("<p>None of the range-specifier values in the Range\n"
  913. "request-header field overlap the current extent\n"
  914. "of the selected resource.</p>\n");
  915. case HTTP_EXPECTATION_FAILED:
  916. s1 = apr_table_get(r->headers_in, "Expect");
  917. if (s1)
  918. s1 = apr_pstrcat(p,
  919. "<p>The expectation given in the Expect request-header\n"
  920. "field could not be met by this server.\n"
  921. "The client sent<pre>\n Expect: ",
  922. ap_escape_html(r->pool, s1), "\n</pre>\n",
  923. NULL);
  924. else
  925. s1 = "<p>No expectation was seen, the Expect request-header \n"
  926. "field was not presented by the client.\n";
  927. return add_optional_notes(r, s1, "error-notes", "</p>"
  928. "<p>Only the 100-continue expectation is supported.</p>\n");
  929. case HTTP_UNPROCESSABLE_ENTITY:
  930. return("<p>The server understands the media type of the\n"
  931. "request entity, but was unable to process the\n"
  932. "contained instructions.</p>\n");
  933. case HTTP_LOCKED:
  934. return("<p>The requested resource is currently locked.\n"
  935. "The lock must be released or proper identification\n"
  936. "given before the method can be applied.</p>\n");
  937. case HTTP_FAILED_DEPENDENCY:
  938. return("<p>The method could not be performed on the resource\n"
  939. "because the requested action depended on another\n"
  940. "action and that other action failed.</p>\n");
  941. case HTTP_UPGRADE_REQUIRED:
  942. return("<p>The requested resource can only be retrieved\n"
  943. "using SSL. The server is willing to upgrade the current\n"
  944. "connection to SSL, but your client doesn't support it.\n"
  945. "Either upgrade your client, or try requesting the page\n"
  946. "using https://\n");
  947. case HTTP_INSUFFICIENT_STORAGE:
  948. return("<p>The method could not be performed on the resource\n"
  949. "because the server is unable to store the\n"
  950. "representation needed to successfully complete the\n"
  951. "request. There is insufficient free space left in\n"
  952. "your storage allocation.</p>\n");
  953. case HTTP_SERVICE_UNAVAILABLE:
  954. return("<p>The server is temporarily unable to service your\n"
  955. "request due to maintenance downtime or capacity\n"
  956. "problems. Please try again later.</p>\n");
  957. case HTTP_GATEWAY_TIME_OUT:
  958. return("<p>The gateway did not receive a timely response\n"
  959. "from the upstream server or application.</p>\n");
  960. case HTTP_NOT_EXTENDED:
  961. return("<p>A mandatory extension policy in the request is not\n"
  962. "accepted by the server for this resource.</p>\n");
  963. default: /* HTTP_INTERNAL_SERVER_ERROR */
  964. /*
  965. * This comparison to expose error-notes could be modified to
  966. * use a configuration directive and export based on that
  967. * directive. For now "*" is used to designate an error-notes
  968. * that is totally safe for any user to see (ie lacks paths,
  969. * database passwords, etc.)
  970. */
  971. if (((error_notes = apr_table_get(r->notes,
  972. "error-notes")) != NULL)
  973. && (h1 = apr_table_get(r->notes, "verbose-error-to")) != NULL
  974. && (strcmp(h1, "*") == 0)) {
  975. return(apr_pstrcat(p, error_notes, "<p />\n", NULL));
  976. }
  977. else {
  978. return(apr_pstrcat(p,
  979. "<p>The server encountered an internal "
  980. "error or\n"
  981. "misconfiguration and was unable to complete\n"
  982. "your request.</p>\n"
  983. "<p>Please contact the server "
  984. "administrator,\n ",
  985. ap_escape_html(r->pool,
  986. r->server->server_admin),
  987. " and inform them of the time the "
  988. "error occurred,\n"
  989. "and anything you might have done that "
  990. "may have\n"
  991. "caused the error.</p>\n"
  992. "<p>More information about this error "
  993. "may be available\n"
  994. "in the server error log.</p>\n",
  995. NULL));
  996. }
  997. /*
  998. * It would be nice to give the user the information they need to
  999. * fix the problem directly since many users don't have access to
  1000. * the error_log (think University sites) even though they can easily
  1001. * get this error by misconfiguring an htaccess file. However, the
  1002. * e error notes tend to include the real file pathname in this case,
  1003. * which some people consider to be a breach of privacy. Until we
  1004. * can figure out a way to remove the pathname, leave this commented.
  1005. *
  1006. * if ((error_notes = apr_table_get(r->notes,
  1007. * "error-notes")) != NULL) {
  1008. * return(apr_pstrcat(p, error_notes, "<p />\n", NULL);
  1009. * }
  1010. * else {
  1011. * return "";
  1012. * }
  1013. */
  1014. }
  1015. }
  1016. /* We should have named this send_canned_response, since it is used for any
  1017. * response that can be generated by the server from the request record.
  1018. * This includes all 204 (no content), 3xx (redirect), 4xx (client error),
  1019. * and 5xx (server error) messages that have not been redirected to another
  1020. * handler via the ErrorDocument feature.
  1021. */
  1022. AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error)
  1023. {
  1024. int status = r->status;
  1025. int idx = ap_index_of_response(status);
  1026. char *custom_response;
  1027. const char *location = apr_table_get(r->headers_out, "Location");
  1028. /* At this point, we are starting the response over, so we have to reset
  1029. * this value.
  1030. */
  1031. r->eos_sent = 0;
  1032. /* and we need to get rid of any RESOURCE filters that might be lurking
  1033. * around, thinking they are in the middle of the original request
  1034. */
  1035. r->output_filters = r->proto_output_filters;
  1036. ap_run_insert_error_filter(r);
  1037. /*
  1038. * It's possible that the Location field might be in r->err_headers_out
  1039. * instead of r->headers_out; use the latter if possible, else the
  1040. * former.
  1041. */
  1042. if (location == NULL) {
  1043. location = apr_table_get(r->err_headers_out, "Location");
  1044. }
  1045. /* We need to special-case the handling of 204 and 304 responses,
  1046. * since they have specific HTTP requirements and do not include a
  1047. * message body. Note that being assbackwards here is not an option.
  1048. */
  1049. if (status == HTTP_NOT_MODIFIED) {
  1050. ap_finalize_request_protocol(r);
  1051. return;
  1052. }
  1053. if (status == HTTP_NO_CONTENT) {
  1054. ap_finalize_request_protocol(r);
  1055. return;
  1056. }
  1057. if (!r->assbackwards) {
  1058. apr_table_t *tmp = r->headers_out;
  1059. /* For all HTTP/1.x responses for which we generate the message,
  1060. * we need to avoid inheriting the "normal status" header fields
  1061. * that may have been set by the request handler before the
  1062. * error or redirect, except for Location on external redirects.
  1063. */
  1064. r->headers_out = r->err_headers_out;
  1065. r->err_headers_out = tmp;
  1066. apr_table_clear(r->err_headers_out);
  1067. if (ap_is_HTTP_REDIRECT(status) || (status == HTTP_CREATED)) {
  1068. if ((location != NULL) && *location) {
  1069. apr_table_setn(r->headers_out, "Location", location);
  1070. }
  1071. else {
  1072. location = ""; /* avoids coredump when printing, below */
  1073. }
  1074. }
  1075. r->content_languages = NULL;
  1076. r->content_encoding = NULL;
  1077. r->clength = 0;
  1078. if (apr_table_get(r->subprocess_env,
  1079. "suppress-error-charset") != NULL) {
  1080. core_request_config *request_conf =
  1081. ap_get_module_config(r->request_config, &core_module);
  1082. request_conf->suppress_charset = 1; /* avoid adding default
  1083. * charset later
  1084. */
  1085. ap_set_content_type(r, "text/html");
  1086. }
  1087. else {
  1088. ap_set_content_type(r, "text/html; charset=iso-8859-1");
  1089. }
  1090. if ((status == HTTP_METHOD_NOT_ALLOWED)
  1091. || (status == HTTP_NOT_IMPLEMENTED)) {
  1092. apr_table_setn(r->headers_out, "Allow", make_allow(r));
  1093. }
  1094. if (r->header_only) {
  1095. ap_finalize_request_protocol(r);
  1096. return;
  1097. }
  1098. }
  1099. if ((custom_response = ap_response_code_string(r, idx))) {
  1100. /*
  1101. * We have a custom response output. This should only be
  1102. * a text-string to write back. But if the ErrorDocument
  1103. * was a local redirect and the requested resource failed
  1104. * for any reason, the custom_response will still hold the
  1105. * redirect URL. We don't really want to output this URL
  1106. * as a text message, so first check the custom response
  1107. * string to ensure that it is a text-string (using the
  1108. * same test used in ap_die(), i.e. does it start with a ").
  1109. *
  1110. * If it's not a text string, we've got a recursive error or
  1111. * an external redirect. If it's a recursive error, ap_die passes
  1112. * us the second error code so we can write both, and has already
  1113. * backed up to the original error. If it's an external redirect,
  1114. * it hasn't happened yet; we may never know if it fails.
  1115. */
  1116. if (custom_response[0] == '\"') {
  1117. ap_rputs(custom_response + 1, r);
  1118. ap_finalize_request_protocol(r);
  1119. return;
  1120. }
  1121. }
  1122. {
  1123. const char *title = status_lines[idx];
  1124. const char *h1;
  1125. /* Accept a status_line set by a module, but only if it begins
  1126. * with the correct 3 digit status code
  1127. */
  1128. if (r->status_line) {
  1129. char *end;
  1130. int len = strlen(r->status_line);
  1131. if (len >= 3
  1132. && apr_strtoi64(r->status_line, &end, 10) == r->status
  1133. && (end - 3) == r->status_line
  1134. && (len < 4 || apr_isspace(r->status_line[3]))
  1135. && (len < 5 || apr_isalnum(r->status_line[4]))) {
  1136. /* Since we passed the above check, we know that length three
  1137. * is equivalent to only a 3 digit numeric http status.
  1138. * RFC2616 mandates a trailing space, let's add it.
  1139. * If we have an empty reason phrase, we also add "Unknown Reason".
  1140. */
  1141. if (len == 3) {
  1142. r->status_line = apr_pstrcat(r->pool, r->status_line, " Unknown Reason", NULL);
  1143. } else if (len == 4) {
  1144. r->status_line = apr_pstrcat(r->pool, r->status_line, "Unknown Reason", NULL);
  1145. }
  1146. title = r->status_line;
  1147. }
  1148. }
  1149. /* folks decided they didn't want the error code in the H1 text */
  1150. h1 = &title[4];
  1151. /* can't count on a charset filter being in place here,
  1152. * so do ebcdic->ascii translation explicitly (if needed)
  1153. */
  1154. ap_rvputs_proto_in_ascii(r,
  1155. DOCTYPE_HTML_2_0
  1156. "<html><head>\n<title>", title,
  1157. "</title>\n</head><body>\n<h1>", h1, "</h1>\n",
  1158. NULL);
  1159. ap_rvputs_proto_in_ascii(r,
  1160. get_canned_error_string(status, r, location),
  1161. NULL);
  1162. if (recursive_error) {
  1163. ap_rvputs_proto_in_ascii(r, "<p>Additionally, a ",
  1164. status_lines[ap_index_of_response(recursive_error)],
  1165. "\nerror was encountered while trying to use an "
  1166. "ErrorDocument to handle the request.</p>\n", NULL);
  1167. }
  1168. ap_rvputs_proto_in_ascii(r, ap_psignature("<hr>\n", r), NULL);
  1169. ap_rvputs_proto_in_ascii(r, "</body></html>\n", NULL);
  1170. }
  1171. ap_finalize_request_protocol(r);
  1172. }
  1173. /*
  1174. * Create a new method list with the specified number of preallocated
  1175. * extension slots.
  1176. */
  1177. AP_DECLARE(ap_method_list_t *) ap_make_method_list(apr_pool_t *p, int nelts)
  1178. {
  1179. ap_method_list_t *ml;
  1180. ml = (ap_method_list_t *) apr_palloc(p, sizeof(ap_method_list_t));
  1181. ml->method_mask = 0;
  1182. ml->method_list = apr_array_make(p, nelts, sizeof(char *));
  1183. return ml;
  1184. }
  1185. /*
  1186. * Make a copy of a method list (primarily for subrequests that may
  1187. * subsequently change it; don't want them changing the parent's, too!).
  1188. */
  1189. AP_DECLARE(void) ap_copy_method_list(ap_method_list_t *dest,
  1190. ap_method_list_t *src)
  1191. {
  1192. int i;
  1193. char **imethods;
  1194. char **omethods;
  1195. dest->method_mask = src->method_mask;
  1196. imethods = (char **) src->method_list->elts;
  1197. for (i = 0; i < src->method_list->nelts; ++i) {
  1198. omethods = (char **) apr_array_push(dest->method_list);
  1199. *omethods = apr_pstrdup(dest->method_list->pool, imethods[i]);
  1200. }
  1201. }
  1202. /*
  1203. * Return true if the specified HTTP method is in the provided
  1204. * method list.
  1205. */
  1206. AP_DECLARE(int) ap_method_in_list(ap_method_list_t *l, const char *method)
  1207. {
  1208. int methnum;
  1209. int i;
  1210. char **methods;
  1211. /*
  1212. * If it's one of our known methods, use the shortcut and check the
  1213. * bitmask.
  1214. */
  1215. methnum = ap_method_number_of(method);
  1216. if (methnum != M_INVALID) {
  1217. return !!(l->method_mask & (AP_METHOD_BIT << methnum));
  1218. }
  1219. /*
  1220. * Otherwise, see if the method name is in the array or string names
  1221. */
  1222. if ((l->method_list == NULL) || (l->method_list->nelts == 0)) {
  1223. return 0;
  1224. }
  1225. methods = (char **)l->method_list->elts;
  1226. for (i = 0; i < l->method_list->nelts; ++i) {
  1227. if (strcmp(method, methods[i]) == 0) {
  1228. return 1;
  1229. }
  1230. }
  1231. return 0;
  1232. }
  1233. /*
  1234. * Add the specified method to a method list (if it isn't already there).
  1235. */
  1236. AP_DECLARE(void) ap_method_list_add(ap_method_list_t *l, const char *method)
  1237. {
  1238. int methnum;
  1239. int i;
  1240. const char **xmethod;
  1241. char **methods;
  1242. /*
  1243. * If it's one of our known methods, use the shortcut and use the
  1244. * bitmask.
  1245. */
  1246. methnum = ap_method_number_of(method);
  1247. l->method_mask |= (AP_METHOD_BIT << methnum);
  1248. if (methnum != M_INVALID) {
  1249. return;
  1250. }
  1251. /*
  1252. * Otherwise, see if the method name is in the array of string names.
  1253. */
  1254. if (l->method_list->nelts != 0) {
  1255. methods = (char **)l->method_list->elts;
  1256. for (i = 0; i < l->method_list->nelts; ++i) {
  1257. if (strcmp(method, methods[i]) == 0) {
  1258. return;
  1259. }
  1260. }
  1261. }
  1262. xmethod = (const char **) apr_array_push(l->method_list);
  1263. *xmethod = method;
  1264. }
  1265. /*
  1266. * Remove the specified method from a method list.
  1267. */
  1268. AP_DECLARE(void) ap_method_list_remove(ap_method_list_t *l,
  1269. const char *method)
  1270. {
  1271. int methnum;
  1272. char **methods;
  1273. /*
  1274. * If it's a known methods, either builtin or registered
  1275. * by a module, use the bitmask.
  1276. */
  1277. methnum = ap_method_number_of(method);
  1278. l->method_mask |= ~(AP_METHOD_BIT << methnum);
  1279. if (methnum != M_INVALID) {
  1280. return;
  1281. }
  1282. /*
  1283. * Otherwise, see if the method name is in the array of string names.
  1284. */
  1285. if (l->method_list->nelts != 0) {
  1286. register int i, j, k;
  1287. methods = (char **)l->method_list->elts;
  1288. for (i = 0; i < l->method_list->nelts; ) {
  1289. if (strcmp(method, methods[i]) == 0) {
  1290. for (j = i, k = i + 1; k < l->method_list->nelts; ++j, ++k) {
  1291. methods[j] = methods[k];
  1292. }
  1293. --l->method_list->nelts;
  1294. }
  1295. else {
  1296. ++i;
  1297. }
  1298. }
  1299. }
  1300. }
  1301. /*
  1302. * Reset a method list to be completely empty.
  1303. */
  1304. AP_DECLARE(void) ap_clear_method_list(ap_method_list_t *l)
  1305. {
  1306. l->method_mask = 0;
  1307. l->method_list->nelts = 0;
  1308. }