http_filters.c.r965824 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632
  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_filter.c --- HTTP routines which either filters or deal with filters.
  18. */
  19. #include "apr.h"
  20. #include "apr_strings.h"
  21. #include "apr_buckets.h"
  22. #include "apr_lib.h"
  23. #include "apr_signal.h"
  24. #define APR_WANT_STDIO /* for sscanf */
  25. #define APR_WANT_STRFUNC
  26. #define APR_WANT_MEMFUNC
  27. #include "apr_want.h"
  28. #define CORE_PRIVATE
  29. #include "util_filter.h"
  30. #include "ap_config.h"
  31. #include "httpd.h"
  32. #include "http_config.h"
  33. #include "http_core.h"
  34. #include "http_protocol.h"
  35. #include "http_main.h"
  36. #include "http_request.h"
  37. #include "http_vhost.h"
  38. #include "http_connection.h"
  39. #include "http_log.h" /* For errors detected in basic auth common
  40. * support code... */
  41. #include "apr_date.h" /* For apr_date_parse_http and APR_DATE_BAD */
  42. #include "util_charset.h"
  43. #include "util_ebcdic.h"
  44. #include "util_time.h"
  45. #include "mod_core.h"
  46. #if APR_HAVE_STDARG_H
  47. #include <stdarg.h>
  48. #endif
  49. #if APR_HAVE_UNISTD_H
  50. #include <unistd.h>
  51. #endif
  52. #define INVALID_CHAR -2
  53. static long get_chunk_size(char *);
  54. typedef struct http_filter_ctx {
  55. apr_off_t remaining;
  56. apr_off_t limit;
  57. apr_off_t limit_used;
  58. enum {
  59. BODY_NONE,
  60. BODY_LENGTH,
  61. BODY_CHUNK,
  62. BODY_CHUNK_PART
  63. } state;
  64. int eos_sent;
  65. char chunk_ln[32];
  66. char *pos;
  67. apr_off_t linesize;
  68. apr_bucket_brigade *bb;
  69. } http_ctx_t;
  70. static apr_status_t bail_out_on_error(http_ctx_t *ctx,
  71. ap_filter_t *f,
  72. int http_error)
  73. {
  74. apr_bucket *e;
  75. apr_bucket_brigade *bb = ctx->bb;
  76. apr_brigade_cleanup(bb);
  77. e = ap_bucket_error_create(http_error,
  78. NULL, f->r->pool,
  79. f->c->bucket_alloc);
  80. APR_BRIGADE_INSERT_TAIL(bb, e);
  81. e = apr_bucket_eos_create(f->c->bucket_alloc);
  82. APR_BRIGADE_INSERT_TAIL(bb, e);
  83. ctx->eos_sent = 1;
  84. return ap_pass_brigade(f->r->output_filters, bb);
  85. }
  86. static apr_status_t get_remaining_chunk_line(http_ctx_t *ctx,
  87. apr_bucket_brigade *b,
  88. int linelimit)
  89. {
  90. apr_status_t rv;
  91. apr_off_t brigade_length;
  92. apr_bucket *e;
  93. const char *lineend;
  94. apr_size_t len;
  95. /*
  96. * As the brigade b should have been requested in mode AP_MODE_GETLINE
  97. * all buckets in this brigade are already some type of memory
  98. * buckets (due to the needed scanning for LF in mode AP_MODE_GETLINE)
  99. * or META buckets.
  100. */
  101. rv = apr_brigade_length(b, 0, &brigade_length);
  102. if (rv != APR_SUCCESS) {
  103. return rv;
  104. }
  105. /* Sanity check. Should never happen. See above. */
  106. if (brigade_length == -1) {
  107. return APR_EGENERAL;
  108. }
  109. if (!brigade_length) {
  110. return APR_EAGAIN;
  111. }
  112. ctx->linesize += brigade_length;
  113. if (ctx->linesize > linelimit) {
  114. return APR_ENOSPC;
  115. }
  116. /*
  117. * As all buckets are already some type of memory buckets or META buckets
  118. * (see above), we only need to check the last byte in the last data bucket.
  119. */
  120. for (e = APR_BRIGADE_LAST(b);
  121. e != APR_BRIGADE_SENTINEL(b);
  122. e = APR_BUCKET_PREV(e)) {
  123. if (APR_BUCKET_IS_METADATA(e)) {
  124. continue;
  125. }
  126. rv = apr_bucket_read(e, &lineend, &len, APR_BLOCK_READ);
  127. if (rv != APR_SUCCESS) {
  128. return rv;
  129. }
  130. if (len > 0) {
  131. break; /* we got the data we want */
  132. }
  133. /* If we got a zero-length data bucket, we try the next one */
  134. }
  135. /* We had no data in this brigade */
  136. if (!len || e == APR_BRIGADE_SENTINEL(b)) {
  137. return APR_EAGAIN;
  138. }
  139. if (lineend[len - 1] != APR_ASCII_LF) {
  140. return APR_EAGAIN;
  141. }
  142. /* Line is complete. So reset ctx->linesize for next round. */
  143. ctx->linesize = 0;
  144. return APR_SUCCESS;
  145. }
  146. static apr_status_t get_chunk_line(http_ctx_t *ctx, apr_bucket_brigade *b,
  147. int linelimit)
  148. {
  149. apr_size_t len;
  150. int tmp_len;
  151. apr_status_t rv;
  152. tmp_len = sizeof(ctx->chunk_ln) - (ctx->pos - ctx->chunk_ln) - 1;
  153. /* Saveguard ourselves against underflows */
  154. if (tmp_len < 0) {
  155. len = 0;
  156. }
  157. else {
  158. len = (apr_size_t) tmp_len;
  159. }
  160. /*
  161. * Check if there is space left in ctx->chunk_ln. If not, then either
  162. * the chunk size is insane or we have chunk-extensions. Ignore both
  163. * by discarding the remaining part of the line via
  164. * get_remaining_chunk_line. Only bail out if the line is too long.
  165. */
  166. if (len > 0) {
  167. rv = apr_brigade_flatten(b, ctx->pos, &len);
  168. if (rv != APR_SUCCESS) {
  169. return rv;
  170. }
  171. ctx->pos += len;
  172. ctx->linesize += len;
  173. *(ctx->pos) = '\0';
  174. /*
  175. * Check if we really got a full line. If yes the
  176. * last char in the just read buffer must be LF.
  177. * If not advance the buffer and return APR_EAGAIN.
  178. * We do not start processing until we have the
  179. * full line.
  180. */
  181. if (ctx->pos[-1] != APR_ASCII_LF) {
  182. /* Check if the remaining data in the brigade has the LF */
  183. return get_remaining_chunk_line(ctx, b, linelimit);
  184. }
  185. /* Line is complete. So reset ctx->pos for next round. */
  186. ctx->pos = ctx->chunk_ln;
  187. return APR_SUCCESS;
  188. }
  189. return get_remaining_chunk_line(ctx, b, linelimit);
  190. }
  191. /* This is the HTTP_INPUT filter for HTTP requests and responses from
  192. * proxied servers (mod_proxy). It handles chunked and content-length
  193. * bodies. This can only be inserted/used after the headers
  194. * are successfully parsed.
  195. */
  196. apr_status_t ap_http_filter(ap_filter_t *f, apr_bucket_brigade *b,
  197. ap_input_mode_t mode, apr_read_type_e block,
  198. apr_off_t readbytes)
  199. {
  200. apr_bucket *e;
  201. http_ctx_t *ctx = f->ctx;
  202. apr_status_t rv;
  203. apr_off_t totalread;
  204. int http_error = HTTP_REQUEST_ENTITY_TOO_LARGE;
  205. apr_bucket_brigade *bb;
  206. /* just get out of the way of things we don't want. */
  207. if (mode != AP_MODE_READBYTES && mode != AP_MODE_GETLINE) {
  208. return ap_get_brigade(f->next, b, mode, block, readbytes);
  209. }
  210. if (!ctx) {
  211. const char *tenc, *lenp;
  212. f->ctx = ctx = apr_pcalloc(f->r->pool, sizeof(*ctx));
  213. ctx->state = BODY_NONE;
  214. ctx->pos = ctx->chunk_ln;
  215. ctx->bb = apr_brigade_create(f->r->pool, f->c->bucket_alloc);
  216. bb = ctx->bb;
  217. /* LimitRequestBody does not apply to proxied responses.
  218. * Consider implementing this check in its own filter.
  219. * Would adding a directive to limit the size of proxied
  220. * responses be useful?
  221. */
  222. if (!f->r->proxyreq) {
  223. ctx->limit = ap_get_limit_req_body(f->r);
  224. }
  225. else {
  226. ctx->limit = 0;
  227. }
  228. tenc = apr_table_get(f->r->headers_in, "Transfer-Encoding");
  229. lenp = apr_table_get(f->r->headers_in, "Content-Length");
  230. if (tenc) {
  231. if (!strcasecmp(tenc, "chunked")) {
  232. ctx->state = BODY_CHUNK;
  233. }
  234. /* test lenp, because it gives another case we can handle */
  235. else if (!lenp) {
  236. /* Something that isn't in HTTP, unless some future
  237. * edition defines new transfer ecodings, is unsupported.
  238. */
  239. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
  240. "Unknown Transfer-Encoding: %s", tenc);
  241. return bail_out_on_error(ctx, f, HTTP_NOT_IMPLEMENTED);
  242. }
  243. else {
  244. ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, f->r,
  245. "Unknown Transfer-Encoding: %s; using Content-Length", tenc);
  246. tenc = NULL;
  247. }
  248. }
  249. if (lenp && !tenc) {
  250. char *endstr;
  251. ctx->state = BODY_LENGTH;
  252. errno = 0;
  253. /* Protects against over/underflow, non-digit chars in the
  254. * string (excluding leading space) (the endstr checks)
  255. * and a negative number. */
  256. if (apr_strtoff(&ctx->remaining, lenp, &endstr, 10)
  257. || endstr == lenp || *endstr || ctx->remaining < 0) {
  258. ctx->remaining = 0;
  259. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
  260. "Invalid Content-Length");
  261. return bail_out_on_error(ctx, f, HTTP_REQUEST_ENTITY_TOO_LARGE);
  262. }
  263. /* If we have a limit in effect and we know the C-L ahead of
  264. * time, stop it here if it is invalid.
  265. */
  266. if (ctx->limit && ctx->limit < ctx->remaining) {
  267. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
  268. "Requested content-length of %" APR_OFF_T_FMT
  269. " is larger than the configured limit"
  270. " of %" APR_OFF_T_FMT, ctx->remaining, ctx->limit);
  271. return bail_out_on_error(ctx, f, HTTP_REQUEST_ENTITY_TOO_LARGE);
  272. }
  273. }
  274. /* If we don't have a request entity indicated by the headers, EOS.
  275. * (BODY_NONE is a valid intermediate state due to trailers,
  276. * but it isn't a valid starting state.)
  277. *
  278. * RFC 2616 Section 4.4 note 5 states that connection-close
  279. * is invalid for a request entity - request bodies must be
  280. * denoted by C-L or T-E: chunked.
  281. *
  282. * Note that since the proxy uses this filter to handle the
  283. * proxied *response*, proxy responses MUST be exempt.
  284. */
  285. if (ctx->state == BODY_NONE && f->r->proxyreq != PROXYREQ_RESPONSE) {
  286. e = apr_bucket_eos_create(f->c->bucket_alloc);
  287. APR_BRIGADE_INSERT_TAIL(b, e);
  288. ctx->eos_sent = 1;
  289. return APR_SUCCESS;
  290. }
  291. /* Since we're about to read data, send 100-Continue if needed.
  292. * Only valid on chunked and C-L bodies where the C-L is > 0. */
  293. if ((ctx->state == BODY_CHUNK ||
  294. (ctx->state == BODY_LENGTH && ctx->remaining > 0)) &&
  295. f->r->expecting_100 && f->r->proto_num >= HTTP_VERSION(1,1) &&
  296. !(f->r->eos_sent || f->r->bytes_sent)) {
  297. if (!ap_is_HTTP_SUCCESS(f->r->status)) {
  298. ctx->state = BODY_NONE;
  299. ctx->eos_sent = 1;
  300. } else {
  301. char *tmp;
  302. int len;
  303. /* if we send an interim response, we're no longer
  304. * in a state of expecting one.
  305. */
  306. f->r->expecting_100 = 0;
  307. tmp = apr_pstrcat(f->r->pool, AP_SERVER_PROTOCOL, " ",
  308. ap_get_status_line(100), CRLF CRLF, NULL);
  309. len = strlen(tmp);
  310. ap_xlate_proto_to_ascii(tmp, len);
  311. apr_brigade_cleanup(bb);
  312. e = apr_bucket_pool_create(tmp, len, f->r->pool,
  313. f->c->bucket_alloc);
  314. APR_BRIGADE_INSERT_HEAD(bb, e);
  315. e = apr_bucket_flush_create(f->c->bucket_alloc);
  316. APR_BRIGADE_INSERT_TAIL(bb, e);
  317. ap_pass_brigade(f->c->output_filters, bb);
  318. }
  319. }
  320. /* We can't read the chunk until after sending 100 if required. */
  321. if (ctx->state == BODY_CHUNK) {
  322. apr_brigade_cleanup(bb);
  323. rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
  324. block, 0);
  325. /* for timeout */
  326. if (block == APR_NONBLOCK_READ &&
  327. ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
  328. (APR_STATUS_IS_EAGAIN(rv)) )) {
  329. ctx->state = BODY_CHUNK_PART;
  330. return APR_EAGAIN;
  331. }
  332. if (rv == APR_SUCCESS) {
  333. rv = get_chunk_line(ctx, bb, f->r->server->limit_req_line);
  334. if (APR_STATUS_IS_EAGAIN(rv)) {
  335. apr_brigade_cleanup(bb);
  336. ctx->state = BODY_CHUNK_PART;
  337. return rv;
  338. }
  339. if (rv == APR_SUCCESS) {
  340. ctx->remaining = get_chunk_size(ctx->chunk_ln);
  341. if (ctx->remaining == INVALID_CHAR) {
  342. rv = APR_EGENERAL;
  343. http_error = HTTP_SERVICE_UNAVAILABLE;
  344. }
  345. }
  346. }
  347. apr_brigade_cleanup(bb);
  348. /* Detect chunksize error (such as overflow) */
  349. if (rv != APR_SUCCESS || ctx->remaining < 0) {
  350. ctx->remaining = 0; /* Reset it in case we have to
  351. * come back here later */
  352. return bail_out_on_error(ctx, f, http_error);
  353. }
  354. if (!ctx->remaining) {
  355. /* Handle trailers by calling ap_get_mime_headers again! */
  356. ctx->state = BODY_NONE;
  357. ap_get_mime_headers(f->r);
  358. e = apr_bucket_eos_create(f->c->bucket_alloc);
  359. APR_BRIGADE_INSERT_TAIL(b, e);
  360. ctx->eos_sent = 1;
  361. return APR_SUCCESS;
  362. }
  363. }
  364. }
  365. else {
  366. bb = ctx->bb;
  367. }
  368. if (ctx->eos_sent) {
  369. e = apr_bucket_eos_create(f->c->bucket_alloc);
  370. APR_BRIGADE_INSERT_TAIL(b, e);
  371. return APR_SUCCESS;
  372. }
  373. if (!ctx->remaining) {
  374. switch (ctx->state) {
  375. case BODY_NONE:
  376. break;
  377. case BODY_LENGTH:
  378. e = apr_bucket_eos_create(f->c->bucket_alloc);
  379. APR_BRIGADE_INSERT_TAIL(b, e);
  380. ctx->eos_sent = 1;
  381. return APR_SUCCESS;
  382. case BODY_CHUNK:
  383. case BODY_CHUNK_PART:
  384. {
  385. apr_brigade_cleanup(bb);
  386. /* We need to read the CRLF after the chunk. */
  387. if (ctx->state == BODY_CHUNK) {
  388. rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
  389. block, 0);
  390. if (block == APR_NONBLOCK_READ &&
  391. ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
  392. (APR_STATUS_IS_EAGAIN(rv)) )) {
  393. return APR_EAGAIN;
  394. }
  395. /* If we get an error, then leave */
  396. if (rv != APR_SUCCESS) {
  397. return rv;
  398. }
  399. /*
  400. * We really don't care whats on this line. If it is RFC
  401. * compliant it should be only \r\n. If there is more
  402. * before we just ignore it as long as we do not get over
  403. * the limit for request lines.
  404. */
  405. rv = get_remaining_chunk_line(ctx, bb,
  406. f->r->server->limit_req_line);
  407. apr_brigade_cleanup(bb);
  408. if (APR_STATUS_IS_EAGAIN(rv)) {
  409. return rv;
  410. }
  411. } else {
  412. rv = APR_SUCCESS;
  413. }
  414. if (rv == APR_SUCCESS) {
  415. /* Read the real chunk line. */
  416. rv = ap_get_brigade(f->next, bb, AP_MODE_GETLINE,
  417. block, 0);
  418. /* Test timeout */
  419. if (block == APR_NONBLOCK_READ &&
  420. ( (rv == APR_SUCCESS && APR_BRIGADE_EMPTY(bb)) ||
  421. (APR_STATUS_IS_EAGAIN(rv)) )) {
  422. ctx->state = BODY_CHUNK_PART;
  423. return APR_EAGAIN;
  424. }
  425. ctx->state = BODY_CHUNK;
  426. if (rv == APR_SUCCESS) {
  427. rv = get_chunk_line(ctx, bb, f->r->server->limit_req_line);
  428. if (APR_STATUS_IS_EAGAIN(rv)) {
  429. ctx->state = BODY_CHUNK_PART;
  430. apr_brigade_cleanup(bb);
  431. return rv;
  432. }
  433. if (rv == APR_SUCCESS) {
  434. ctx->remaining = get_chunk_size(ctx->chunk_ln);
  435. if (ctx->remaining == INVALID_CHAR) {
  436. rv = APR_EGENERAL;
  437. http_error = HTTP_SERVICE_UNAVAILABLE;
  438. }
  439. }
  440. }
  441. apr_brigade_cleanup(bb);
  442. }
  443. /* Detect chunksize error (such as overflow) */
  444. if (rv != APR_SUCCESS || ctx->remaining < 0) {
  445. ctx->remaining = 0; /* Reset it in case we have to
  446. * come back here later */
  447. bail_out_on_error(ctx, f, http_error);
  448. return rv;
  449. }
  450. if (!ctx->remaining) {
  451. /* Handle trailers by calling ap_get_mime_headers again! */
  452. ctx->state = BODY_NONE;
  453. ap_get_mime_headers(f->r);
  454. e = apr_bucket_eos_create(f->c->bucket_alloc);
  455. APR_BRIGADE_INSERT_TAIL(b, e);
  456. ctx->eos_sent = 1;
  457. return APR_SUCCESS;
  458. }
  459. }
  460. break;
  461. }
  462. }
  463. /* Ensure that the caller can not go over our boundary point. */
  464. if (ctx->state == BODY_LENGTH || ctx->state == BODY_CHUNK) {
  465. if (ctx->remaining < readbytes) {
  466. readbytes = ctx->remaining;
  467. }
  468. AP_DEBUG_ASSERT(readbytes > 0);
  469. }
  470. rv = ap_get_brigade(f->next, b, mode, block, readbytes);
  471. if (rv != APR_SUCCESS) {
  472. return rv;
  473. }
  474. /* How many bytes did we just read? */
  475. apr_brigade_length(b, 0, &totalread);
  476. /* If this happens, we have a bucket of unknown length. Die because
  477. * it means our assumptions have changed. */
  478. AP_DEBUG_ASSERT(totalread >= 0);
  479. if (ctx->state != BODY_NONE) {
  480. ctx->remaining -= totalread;
  481. if (ctx->remaining > 0) {
  482. e = APR_BRIGADE_LAST(b);
  483. if (APR_BUCKET_IS_EOS(e))
  484. return APR_EOF;
  485. }
  486. }
  487. /* If we have no more bytes remaining on a C-L request,
  488. * save the callter a roundtrip to discover EOS.
  489. */
  490. if (ctx->state == BODY_LENGTH && ctx->remaining == 0) {
  491. e = apr_bucket_eos_create(f->c->bucket_alloc);
  492. APR_BRIGADE_INSERT_TAIL(b, e);
  493. }
  494. /* We have a limit in effect. */
  495. if (ctx->limit) {
  496. /* FIXME: Note that we might get slightly confused on chunked inputs
  497. * as we'd need to compensate for the chunk lengths which may not
  498. * really count. This seems to be up for interpretation. */
  499. ctx->limit_used += totalread;
  500. if (ctx->limit < ctx->limit_used) {
  501. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r,
  502. "Read content-length of %" APR_OFF_T_FMT
  503. " is larger than the configured limit"
  504. " of %" APR_OFF_T_FMT, ctx->limit_used, ctx->limit);
  505. apr_brigade_cleanup(bb);
  506. e = ap_bucket_error_create(HTTP_REQUEST_ENTITY_TOO_LARGE, NULL,
  507. f->r->pool,
  508. f->c->bucket_alloc);
  509. APR_BRIGADE_INSERT_TAIL(bb, e);
  510. e = apr_bucket_eos_create(f->c->bucket_alloc);
  511. APR_BRIGADE_INSERT_TAIL(bb, e);
  512. ctx->eos_sent = 1;
  513. return ap_pass_brigade(f->r->output_filters, bb);
  514. }
  515. }
  516. return APR_SUCCESS;
  517. }
  518. /**
  519. * Parse a chunk extension, detect overflow.
  520. * There are two error cases:
  521. * 1) If the conversion would require too many bits, a -1 is returned.
  522. * 2) If the conversion used the correct number of bits, but an overflow
  523. * caused only the sign bit to flip, then that negative number is
  524. * returned.
  525. * In general, any negative number can be considered an overflow error.
  526. */
  527. static long get_chunk_size(char *b)
  528. {
  529. long chunksize = 0;
  530. size_t chunkbits = sizeof(long) * 8;
  531. ap_xlate_proto_from_ascii(b, strlen(b));
  532. if (!apr_isxdigit(*b)) {
  533. /*
  534. * Detect invalid character at beginning. This also works for empty
  535. * chunk size lines.
  536. */
  537. return INVALID_CHAR;
  538. }
  539. /* Skip leading zeros */
  540. while (*b == '0') {
  541. ++b;
  542. }
  543. while (apr_isxdigit(*b) && (chunkbits > 0)) {
  544. int xvalue = 0;
  545. if (*b >= '0' && *b <= '9') {
  546. xvalue = *b - '0';
  547. }
  548. else if (*b >= 'A' && *b <= 'F') {
  549. xvalue = *b - 'A' + 0xa;
  550. }
  551. else if (*b >= 'a' && *b <= 'f') {
  552. xvalue = *b - 'a' + 0xa;
  553. }
  554. chunksize = (chunksize << 4) | xvalue;
  555. chunkbits -= 4;
  556. ++b;
  557. }
  558. if (apr_isxdigit(*b) && (chunkbits <= 0)) {
  559. /* overflow */
  560. return -1;
  561. }
  562. return chunksize;
  563. }
  564. typedef struct header_struct {
  565. apr_pool_t *pool;
  566. apr_bucket_brigade *bb;
  567. } header_struct;
  568. /* Send a single HTTP header field to the client. Note that this function
  569. * is used in calls to table_do(), so their interfaces are co-dependent.
  570. * In other words, don't change this one without checking table_do in alloc.c.
  571. * It returns true unless there was a write error of some kind.
  572. */
  573. static int form_header_field(header_struct *h,
  574. const char *fieldname, const char *fieldval)
  575. {
  576. #if APR_CHARSET_EBCDIC
  577. char *headfield;
  578. apr_size_t len;
  579. apr_size_t name_len;
  580. apr_size_t val_len;
  581. char *next;
  582. name_len = strlen(fieldname);
  583. val_len = strlen(fieldval);
  584. len = name_len + val_len + 4; /* 4 for ": " plus CRLF */
  585. headfield = (char *)apr_palloc(h->pool, len + 1);
  586. memcpy(headfield, fieldname, name_len);
  587. next = headfield + name_len;
  588. *next++ = ':';
  589. *next++ = ' ';
  590. memcpy(next, fieldval, val_len);
  591. next += val_len;
  592. *next++ = CR;
  593. *next++ = LF;
  594. *next = 0;
  595. ap_xlate_proto_to_ascii(headfield, len);
  596. apr_brigade_write(h->bb, NULL, NULL, headfield, len);
  597. #else
  598. struct iovec vec[4];
  599. struct iovec *v = vec;
  600. v->iov_base = (void *)fieldname;
  601. v->iov_len = strlen(fieldname);
  602. v++;
  603. v->iov_base = ": ";
  604. v->iov_len = sizeof(": ") - 1;
  605. v++;
  606. v->iov_base = (void *)fieldval;
  607. v->iov_len = strlen(fieldval);
  608. v++;
  609. v->iov_base = CRLF;
  610. v->iov_len = sizeof(CRLF) - 1;
  611. apr_brigade_writev(h->bb, NULL, NULL, vec, 4);
  612. #endif /* !APR_CHARSET_EBCDIC */
  613. return 1;
  614. }
  615. /* This routine is called by apr_table_do and merges all instances of
  616. * the passed field values into a single array that will be further
  617. * processed by some later routine. Originally intended to help split
  618. * and recombine multiple Vary fields, though it is generic to any field
  619. * consisting of comma/space-separated tokens.
  620. */
  621. static int uniq_field_values(void *d, const char *key, const char *val)
  622. {
  623. apr_array_header_t *values;
  624. char *start;
  625. char *e;
  626. char **strpp;
  627. int i;
  628. values = (apr_array_header_t *)d;
  629. e = apr_pstrdup(values->pool, val);
  630. do {
  631. /* Find a non-empty fieldname */
  632. while (*e == ',' || apr_isspace(*e)) {
  633. ++e;
  634. }
  635. if (*e == '\0') {
  636. break;
  637. }
  638. start = e;
  639. while (*e != '\0' && *e != ',' && !apr_isspace(*e)) {
  640. ++e;
  641. }
  642. if (*e != '\0') {
  643. *e++ = '\0';
  644. }
  645. /* Now add it to values if it isn't already represented.
  646. * Could be replaced by a ap_array_strcasecmp() if we had one.
  647. */
  648. for (i = 0, strpp = (char **) values->elts; i < values->nelts;
  649. ++i, ++strpp) {
  650. if (*strpp && strcasecmp(*strpp, start) == 0) {
  651. break;
  652. }
  653. }
  654. if (i == values->nelts) { /* if not found */
  655. *(char **)apr_array_push(values) = start;
  656. }
  657. } while (*e != '\0');
  658. return 1;
  659. }
  660. /*
  661. * Since some clients choke violently on multiple Vary fields, or
  662. * Vary fields with duplicate tokens, combine any multiples and remove
  663. * any duplicates.
  664. */
  665. static void fixup_vary(request_rec *r)
  666. {
  667. apr_array_header_t *varies;
  668. varies = apr_array_make(r->pool, 5, sizeof(char *));
  669. /* Extract all Vary fields from the headers_out, separate each into
  670. * its comma-separated fieldname values, and then add them to varies
  671. * if not already present in the array.
  672. */
  673. apr_table_do((int (*)(void *, const char *, const char *))uniq_field_values,
  674. (void *) varies, r->headers_out, "Vary", NULL);
  675. /* If we found any, replace old Vary fields with unique-ified value */
  676. if (varies->nelts > 0) {
  677. apr_table_setn(r->headers_out, "Vary",
  678. apr_array_pstrcat(r->pool, varies, ','));
  679. }
  680. }
  681. /* Send a request's HTTP response headers to the client.
  682. */
  683. static apr_status_t send_all_header_fields(header_struct *h,
  684. const request_rec *r)
  685. {
  686. const apr_array_header_t *elts;
  687. const apr_table_entry_t *t_elt;
  688. const apr_table_entry_t *t_end;
  689. struct iovec *vec;
  690. struct iovec *vec_next;
  691. elts = apr_table_elts(r->headers_out);
  692. if (elts->nelts == 0) {
  693. return APR_SUCCESS;
  694. }
  695. t_elt = (const apr_table_entry_t *)(elts->elts);
  696. t_end = t_elt + elts->nelts;
  697. vec = (struct iovec *)apr_palloc(h->pool, 4 * elts->nelts *
  698. sizeof(struct iovec));
  699. vec_next = vec;
  700. /* For each field, generate
  701. * name ": " value CRLF
  702. */
  703. do {
  704. vec_next->iov_base = (void*)(t_elt->key);
  705. vec_next->iov_len = strlen(t_elt->key);
  706. vec_next++;
  707. vec_next->iov_base = ": ";
  708. vec_next->iov_len = sizeof(": ") - 1;
  709. vec_next++;
  710. vec_next->iov_base = (void*)(t_elt->val);
  711. vec_next->iov_len = strlen(t_elt->val);
  712. vec_next++;
  713. vec_next->iov_base = CRLF;
  714. vec_next->iov_len = sizeof(CRLF) - 1;
  715. vec_next++;
  716. t_elt++;
  717. } while (t_elt < t_end);
  718. #if APR_CHARSET_EBCDIC
  719. {
  720. apr_size_t len;
  721. char *tmp = apr_pstrcatv(r->pool, vec, vec_next - vec, &len);
  722. ap_xlate_proto_to_ascii(tmp, len);
  723. return apr_brigade_write(h->bb, NULL, NULL, tmp, len);
  724. }
  725. #else
  726. return apr_brigade_writev(h->bb, NULL, NULL, vec, vec_next - vec);
  727. #endif
  728. }
  729. /* Confirm that the status line is well-formed and matches r->status.
  730. * If they don't match, a filter may have negated the status line set by a
  731. * handler.
  732. * Zap r->status_line if bad.
  733. */
  734. static void validate_status_line(request_rec *r)
  735. {
  736. char *end;
  737. if (r->status_line
  738. && (strlen(r->status_line) <= 4
  739. || apr_strtoi64(r->status_line, &end, 10) != r->status
  740. || *end != ' '
  741. || (end - 3) != r->status_line)) {
  742. r->status_line = NULL;
  743. }
  744. }
  745. /*
  746. * Determine the protocol to use for the response. Potentially downgrade
  747. * to HTTP/1.0 in some situations and/or turn off keepalives.
  748. *
  749. * also prepare r->status_line.
  750. */
  751. static void basic_http_header_check(request_rec *r,
  752. const char **protocol)
  753. {
  754. if (r->assbackwards) {
  755. /* no such thing as a response protocol */
  756. return;
  757. }
  758. validate_status_line(r);
  759. if (!r->status_line) {
  760. r->status_line = ap_get_status_line(r->status);
  761. }
  762. /* Note that we must downgrade before checking for force responses. */
  763. if (r->proto_num > HTTP_VERSION(1,0)
  764. && apr_table_get(r->subprocess_env, "downgrade-1.0")) {
  765. r->proto_num = HTTP_VERSION(1,0);
  766. }
  767. /* kludge around broken browsers when indicated by force-response-1.0
  768. */
  769. if (r->proto_num == HTTP_VERSION(1,0)
  770. && apr_table_get(r->subprocess_env, "force-response-1.0")) {
  771. *protocol = "HTTP/1.0";
  772. r->connection->keepalive = AP_CONN_CLOSE;
  773. }
  774. else {
  775. *protocol = AP_SERVER_PROTOCOL;
  776. }
  777. }
  778. /* fill "bb" with a barebones/initial HTTP response header */
  779. static void basic_http_header(request_rec *r, apr_bucket_brigade *bb,
  780. const char *protocol)
  781. {
  782. char *date;
  783. const char *server;
  784. header_struct h;
  785. struct iovec vec[4];
  786. if (r->assbackwards) {
  787. /* there are no headers to send */
  788. return;
  789. }
  790. /* Output the HTTP/1.x Status-Line and the Date and Server fields */
  791. vec[0].iov_base = (void *)protocol;
  792. vec[0].iov_len = strlen(protocol);
  793. vec[1].iov_base = (void *)" ";
  794. vec[1].iov_len = sizeof(" ") - 1;
  795. vec[2].iov_base = (void *)(r->status_line);
  796. vec[2].iov_len = strlen(r->status_line);
  797. vec[3].iov_base = (void *)CRLF;
  798. vec[3].iov_len = sizeof(CRLF) - 1;
  799. #if APR_CHARSET_EBCDIC
  800. {
  801. char *tmp;
  802. apr_size_t len;
  803. tmp = apr_pstrcatv(r->pool, vec, 4, &len);
  804. ap_xlate_proto_to_ascii(tmp, len);
  805. apr_brigade_write(bb, NULL, NULL, tmp, len);
  806. }
  807. #else
  808. apr_brigade_writev(bb, NULL, NULL, vec, 4);
  809. #endif
  810. h.pool = r->pool;
  811. h.bb = bb;
  812. /*
  813. * keep the set-by-proxy server and date headers, otherwise
  814. * generate a new server header / date header
  815. */
  816. if (r->proxyreq != PROXYREQ_NONE) {
  817. const char *proxy_date;
  818. proxy_date = apr_table_get(r->headers_out, "Date");
  819. if (!proxy_date) {
  820. /*
  821. * proxy_date needs to be const. So use date for the creation of
  822. * our own Date header and pass it over to proxy_date later to
  823. * avoid a compiler warning.
  824. */
  825. date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
  826. ap_recent_rfc822_date(date, r->request_time);
  827. proxy_date = date;
  828. }
  829. form_header_field(&h, "Date", proxy_date);
  830. server = apr_table_get(r->headers_out, "Server");
  831. if (server) {
  832. form_header_field(&h, "Server", server);
  833. }
  834. }
  835. else {
  836. date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
  837. ap_recent_rfc822_date(date, r->request_time);
  838. form_header_field(&h, "Date", date);
  839. form_header_field(&h, "Server", ap_get_server_banner());
  840. }
  841. /* unset so we don't send them again */
  842. apr_table_unset(r->headers_out, "Date"); /* Avoid bogosity */
  843. apr_table_unset(r->headers_out, "Server");
  844. }
  845. AP_DECLARE(void) ap_basic_http_header(request_rec *r, apr_bucket_brigade *bb)
  846. {
  847. const char *protocol;
  848. basic_http_header_check(r, &protocol);
  849. basic_http_header(r, bb, protocol);
  850. }
  851. static void terminate_header(apr_bucket_brigade *bb)
  852. {
  853. char crlf[] = CRLF;
  854. apr_size_t buflen;
  855. buflen = strlen(crlf);
  856. ap_xlate_proto_to_ascii(crlf, buflen);
  857. apr_brigade_write(bb, NULL, NULL, crlf, buflen);
  858. }
  859. AP_DECLARE_NONSTD(int) ap_send_http_trace(request_rec *r)
  860. {
  861. core_server_config *conf;
  862. int rv;
  863. apr_bucket_brigade *bb;
  864. header_struct h;
  865. apr_bucket *b;
  866. int body;
  867. char *bodyread = NULL, *bodyoff;
  868. apr_size_t bodylen = 0;
  869. apr_size_t bodybuf;
  870. long res = -1; /* init to avoid gcc -Wall warning */
  871. if (r->method_number != M_TRACE) {
  872. return DECLINED;
  873. }
  874. /* Get the original request */
  875. while (r->prev) {
  876. r = r->prev;
  877. }
  878. conf = (core_server_config *)ap_get_module_config(r->server->module_config,
  879. &core_module);
  880. if (conf->trace_enable == AP_TRACE_DISABLE) {
  881. apr_table_setn(r->notes, "error-notes",
  882. "TRACE denied by server configuration");
  883. return HTTP_METHOD_NOT_ALLOWED;
  884. }
  885. if (conf->trace_enable == AP_TRACE_EXTENDED)
  886. /* XX should be = REQUEST_CHUNKED_PASS */
  887. body = REQUEST_CHUNKED_DECHUNK;
  888. else
  889. body = REQUEST_NO_BODY;
  890. if ((rv = ap_setup_client_block(r, body))) {
  891. if (rv == HTTP_REQUEST_ENTITY_TOO_LARGE)
  892. apr_table_setn(r->notes, "error-notes",
  893. "TRACE with a request body is not allowed");
  894. return rv;
  895. }
  896. if (ap_should_client_block(r)) {
  897. if (r->remaining > 0) {
  898. if (r->remaining > 65536) {
  899. apr_table_setn(r->notes, "error-notes",
  900. "Extended TRACE request bodies cannot exceed 64k\n");
  901. return HTTP_REQUEST_ENTITY_TOO_LARGE;
  902. }
  903. /* always 32 extra bytes to catch chunk header exceptions */
  904. bodybuf = (apr_size_t)r->remaining + 32;
  905. }
  906. else {
  907. /* Add an extra 8192 for chunk headers */
  908. bodybuf = 73730;
  909. }
  910. bodyoff = bodyread = apr_palloc(r->pool, bodybuf);
  911. /* only while we have enough for a chunked header */
  912. while ((!bodylen || bodybuf >= 32) &&
  913. (res = ap_get_client_block(r, bodyoff, bodybuf)) > 0) {
  914. bodylen += res;
  915. bodybuf -= res;
  916. bodyoff += res;
  917. }
  918. if (res > 0 && bodybuf < 32) {
  919. /* discard_rest_of_request_body into our buffer */
  920. while (ap_get_client_block(r, bodyread, bodylen) > 0)
  921. ;
  922. apr_table_setn(r->notes, "error-notes",
  923. "Extended TRACE request bodies cannot exceed 64k\n");
  924. return HTTP_REQUEST_ENTITY_TOO_LARGE;
  925. }
  926. if (res < 0) {
  927. return HTTP_BAD_REQUEST;
  928. }
  929. }
  930. ap_set_content_type(r, "message/http");
  931. /* Now we recreate the request, and echo it back */
  932. bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  933. #if APR_CHARSET_EBCDIC
  934. {
  935. char *tmp;
  936. apr_size_t len;
  937. len = strlen(r->the_request);
  938. tmp = apr_pmemdup(r->pool, r->the_request, len);
  939. ap_xlate_proto_to_ascii(tmp, len);
  940. apr_brigade_putstrs(bb, NULL, NULL, tmp, CRLF_ASCII, NULL);
  941. }
  942. #else
  943. apr_brigade_putstrs(bb, NULL, NULL, r->the_request, CRLF, NULL);
  944. #endif
  945. h.pool = r->pool;
  946. h.bb = bb;
  947. apr_table_do((int (*) (void *, const char *, const char *))
  948. form_header_field, (void *) &h, r->headers_in, NULL);
  949. apr_brigade_puts(bb, NULL, NULL, CRLF_ASCII);
  950. /* If configured to accept a body, echo the body */
  951. if (bodylen) {
  952. b = apr_bucket_pool_create(bodyread, bodylen,
  953. r->pool, bb->bucket_alloc);
  954. APR_BRIGADE_INSERT_TAIL(bb, b);
  955. }
  956. ap_pass_brigade(r->output_filters, bb);
  957. return DONE;
  958. }
  959. typedef struct header_filter_ctx {
  960. int headers_sent;
  961. } header_filter_ctx;
  962. AP_CORE_DECLARE_NONSTD(apr_status_t) ap_http_header_filter(ap_filter_t *f,
  963. apr_bucket_brigade *b)
  964. {
  965. request_rec *r = f->r;
  966. conn_rec *c = r->connection;
  967. const char *clheader;
  968. const char *protocol;
  969. apr_bucket *e;
  970. apr_bucket_brigade *b2;
  971. header_struct h;
  972. header_filter_ctx *ctx = f->ctx;
  973. const char *ctype;
  974. ap_bucket_error *eb = NULL;
  975. AP_DEBUG_ASSERT(!r->main);
  976. if (r->header_only) {
  977. if (!ctx) {
  978. ctx = f->ctx = apr_pcalloc(r->pool, sizeof(header_filter_ctx));
  979. }
  980. else if (ctx->headers_sent) {
  981. apr_brigade_cleanup(b);
  982. return OK;
  983. }
  984. }
  985. for (e = APR_BRIGADE_FIRST(b);
  986. e != APR_BRIGADE_SENTINEL(b);
  987. e = APR_BUCKET_NEXT(e))
  988. {
  989. if (AP_BUCKET_IS_ERROR(e) && !eb) {
  990. eb = e->data;
  991. continue;
  992. }
  993. /*
  994. * If we see an EOC bucket it is a signal that we should get out
  995. * of the way doing nothing.
  996. */
  997. if (AP_BUCKET_IS_EOC(e)) {
  998. ap_remove_output_filter(f);
  999. return ap_pass_brigade(f->next, b);
  1000. }
  1001. }
  1002. if (eb) {
  1003. int status;
  1004. status = eb->status;
  1005. apr_brigade_cleanup(b);
  1006. ap_die(status, r);
  1007. return AP_FILTER_ERROR;
  1008. }
  1009. if (r->assbackwards) {
  1010. r->sent_bodyct = 1;
  1011. ap_remove_output_filter(f);
  1012. return ap_pass_brigade(f->next, b);
  1013. }
  1014. /*
  1015. * Now that we are ready to send a response, we need to combine the two
  1016. * header field tables into a single table. If we don't do this, our
  1017. * later attempts to set or unset a given fieldname might be bypassed.
  1018. */
  1019. if (!apr_is_empty_table(r->err_headers_out)) {
  1020. r->headers_out = apr_table_overlay(r->pool, r->err_headers_out,
  1021. r->headers_out);
  1022. }
  1023. /*
  1024. * Remove the 'Vary' header field if the client can't handle it.
  1025. * Since this will have nasty effects on HTTP/1.1 caches, force
  1026. * the response into HTTP/1.0 mode.
  1027. *
  1028. * Note: the force-response-1.0 should come before the call to
  1029. * basic_http_header_check()
  1030. */
  1031. if (apr_table_get(r->subprocess_env, "force-no-vary") != NULL) {
  1032. apr_table_unset(r->headers_out, "Vary");
  1033. r->proto_num = HTTP_VERSION(1,0);
  1034. apr_table_set(r->subprocess_env, "force-response-1.0", "1");
  1035. }
  1036. else {
  1037. fixup_vary(r);
  1038. }
  1039. /*
  1040. * Now remove any ETag response header field if earlier processing
  1041. * says so (such as a 'FileETag None' directive).
  1042. */
  1043. if (apr_table_get(r->notes, "no-etag") != NULL) {
  1044. apr_table_unset(r->headers_out, "ETag");
  1045. }
  1046. /* determine the protocol and whether we should use keepalives. */
  1047. basic_http_header_check(r, &protocol);
  1048. ap_set_keepalive(r);
  1049. if (r->chunked) {
  1050. apr_table_mergen(r->headers_out, "Transfer-Encoding", "chunked");
  1051. apr_table_unset(r->headers_out, "Content-Length");
  1052. }
  1053. ctype = ap_make_content_type(r, r->content_type);
  1054. if (strcasecmp(ctype, NO_CONTENT_TYPE)) {
  1055. apr_table_setn(r->headers_out, "Content-Type", ctype);
  1056. }
  1057. if (r->content_encoding) {
  1058. apr_table_setn(r->headers_out, "Content-Encoding",
  1059. r->content_encoding);
  1060. }
  1061. if (!apr_is_empty_array(r->content_languages)) {
  1062. int i;
  1063. char *token;
  1064. char **languages = (char **)(r->content_languages->elts);
  1065. const char *field = apr_table_get(r->headers_out, "Content-Language");
  1066. while (field && (token = ap_get_list_item(r->pool, &field)) != NULL) {
  1067. for (i = 0; i < r->content_languages->nelts; ++i) {
  1068. if (!strcasecmp(token, languages[i]))
  1069. break;
  1070. }
  1071. if (i == r->content_languages->nelts) {
  1072. *((char **) apr_array_push(r->content_languages)) = token;
  1073. }
  1074. }
  1075. field = apr_array_pstrcat(r->pool, r->content_languages, ',');
  1076. apr_table_setn(r->headers_out, "Content-Language", field);
  1077. }
  1078. /*
  1079. * Control cachability for non-cachable responses if not already set by
  1080. * some other part of the server configuration.
  1081. */
  1082. if (r->no_cache && !apr_table_get(r->headers_out, "Expires")) {
  1083. char *date = apr_palloc(r->pool, APR_RFC822_DATE_LEN);
  1084. ap_recent_rfc822_date(date, r->request_time);
  1085. apr_table_addn(r->headers_out, "Expires", date);
  1086. }
  1087. /* This is a hack, but I can't find anyway around it. The idea is that
  1088. * we don't want to send out 0 Content-Lengths if it is a head request.
  1089. * This happens when modules try to outsmart the server, and return
  1090. * if they see a HEAD request. Apache 1.3 handlers were supposed to
  1091. * just return in that situation, and the core handled the HEAD. In
  1092. * 2.0, if a handler returns, then the core sends an EOS bucket down
  1093. * the filter stack, and the content-length filter computes a C-L of
  1094. * zero and that gets put in the headers, and we end up sending a
  1095. * zero C-L to the client. We can't just remove the C-L filter,
  1096. * because well behaved 2.0 handlers will send their data down the stack,
  1097. * and we will compute a real C-L for the head request. RBB
  1098. */
  1099. if (r->header_only
  1100. && (clheader = apr_table_get(r->headers_out, "Content-Length"))
  1101. && !strcmp(clheader, "0")) {
  1102. apr_table_unset(r->headers_out, "Content-Length");
  1103. }
  1104. b2 = apr_brigade_create(r->pool, c->bucket_alloc);
  1105. basic_http_header(r, b2, protocol);
  1106. h.pool = r->pool;
  1107. h.bb = b2;
  1108. if (r->status == HTTP_NOT_MODIFIED) {
  1109. apr_table_do((int (*)(void *, const char *, const char *)) form_header_field,
  1110. (void *) &h, r->headers_out,
  1111. "Connection",
  1112. "Keep-Alive",
  1113. "ETag",
  1114. "Content-Location",
  1115. "Expires",
  1116. "Cache-Control",
  1117. "Vary",
  1118. "Warning",
  1119. "WWW-Authenticate",
  1120. "Proxy-Authenticate",
  1121. "Set-Cookie",
  1122. "Set-Cookie2",
  1123. NULL);
  1124. }
  1125. else {
  1126. send_all_header_fields(&h, r);
  1127. }
  1128. terminate_header(b2);
  1129. ap_pass_brigade(f->next, b2);
  1130. if (r->header_only) {
  1131. apr_brigade_cleanup(b);
  1132. ctx->headers_sent = 1;
  1133. return OK;
  1134. }
  1135. r->sent_bodyct = 1; /* Whatever follows is real body stuff... */
  1136. if (r->chunked) {
  1137. /* We can't add this filter until we have already sent the headers.
  1138. * If we add it before this point, then the headers will be chunked
  1139. * as well, and that is just wrong.
  1140. */
  1141. ap_add_output_filter("CHUNK", NULL, r, r->connection);
  1142. }
  1143. /* Don't remove this filter until after we have added the CHUNK filter.
  1144. * Otherwise, f->next won't be the CHUNK filter and thus the first
  1145. * brigade won't be chunked properly.
  1146. */
  1147. ap_remove_output_filter(f);
  1148. return ap_pass_brigade(f->next, b);
  1149. }
  1150. /* In HTTP/1.1, any method can have a body. However, most GET handlers
  1151. * wouldn't know what to do with a request body if they received one.
  1152. * This helper routine tests for and reads any message body in the request,
  1153. * simply discarding whatever it receives. We need to do this because
  1154. * failing to read the request body would cause it to be interpreted
  1155. * as the next request on a persistent connection.
  1156. *
  1157. * Since we return an error status if the request is malformed, this
  1158. * routine should be called at the beginning of a no-body handler, e.g.,
  1159. *
  1160. * if ((retval = ap_discard_request_body(r)) != OK) {
  1161. * return retval;
  1162. * }
  1163. */
  1164. AP_DECLARE(int) ap_discard_request_body(request_rec *r)
  1165. {
  1166. apr_bucket_brigade *bb;
  1167. int rv, seen_eos;
  1168. /* Sometimes we'll get in a state where the input handling has
  1169. * detected an error where we want to drop the connection, so if
  1170. * that's the case, don't read the data as that is what we're trying
  1171. * to avoid.
  1172. *
  1173. * This function is also a no-op on a subrequest.
  1174. */
  1175. if (r->main || r->connection->keepalive == AP_CONN_CLOSE ||
  1176. ap_status_drops_connection(r->status)) {
  1177. return OK;
  1178. }
  1179. bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  1180. seen_eos = 0;
  1181. do {
  1182. apr_bucket *bucket;
  1183. rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
  1184. APR_BLOCK_READ, HUGE_STRING_LEN);
  1185. if (rv != APR_SUCCESS) {
  1186. /* FIXME: If we ever have a mapping from filters (apr_status_t)
  1187. * to HTTP error codes, this would be a good place for them.
  1188. *
  1189. * If we received the special case AP_FILTER_ERROR, it means
  1190. * that the filters have already handled this error.
  1191. * Otherwise, we should assume we have a bad request.
  1192. */
  1193. if (rv == AP_FILTER_ERROR) {
  1194. apr_brigade_destroy(bb);
  1195. return rv;
  1196. }
  1197. else {
  1198. apr_brigade_destroy(bb);
  1199. return HTTP_BAD_REQUEST;
  1200. }
  1201. }
  1202. for (bucket = APR_BRIGADE_FIRST(bb);
  1203. bucket != APR_BRIGADE_SENTINEL(bb);
  1204. bucket = APR_BUCKET_NEXT(bucket))
  1205. {
  1206. const char *data;
  1207. apr_size_t len;
  1208. if (APR_BUCKET_IS_EOS(bucket)) {
  1209. seen_eos = 1;
  1210. break;
  1211. }
  1212. /* These are metadata buckets. */
  1213. if (bucket->length == 0) {
  1214. continue;
  1215. }
  1216. /* We MUST read because in case we have an unknown-length
  1217. * bucket or one that morphs, we want to exhaust it.
  1218. */
  1219. rv = apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ);
  1220. if (rv != APR_SUCCESS) {
  1221. apr_brigade_destroy(bb);
  1222. return HTTP_BAD_REQUEST;
  1223. }
  1224. }
  1225. apr_brigade_cleanup(bb);
  1226. } while (!seen_eos);
  1227. return OK;
  1228. }
  1229. /* Here we deal with getting the request message body from the client.
  1230. * Whether or not the request contains a body is signaled by the presence
  1231. * of a non-zero Content-Length or by a Transfer-Encoding: chunked.
  1232. *
  1233. * Note that this is more complicated than it was in Apache 1.1 and prior
  1234. * versions, because chunked support means that the module does less.
  1235. *
  1236. * The proper procedure is this:
  1237. *
  1238. * 1. Call ap_setup_client_block() near the beginning of the request
  1239. * handler. This will set up all the necessary properties, and will
  1240. * return either OK, or an error code. If the latter, the module should
  1241. * return that error code. The second parameter selects the policy to
  1242. * apply if the request message indicates a body, and how a chunked
  1243. * transfer-coding should be interpreted. Choose one of
  1244. *
  1245. * REQUEST_NO_BODY Send 413 error if message has any body
  1246. * REQUEST_CHUNKED_ERROR Send 411 error if body without Content-Length
  1247. * REQUEST_CHUNKED_DECHUNK If chunked, remove the chunks for me.
  1248. * REQUEST_CHUNKED_PASS If chunked, pass the chunk headers with body.
  1249. *
  1250. * In order to use the last two options, the caller MUST provide a buffer
  1251. * large enough to hold a chunk-size line, including any extensions.
  1252. *
  1253. * 2. When you are ready to read a body (if any), call ap_should_client_block().
  1254. * This will tell the module whether or not to read input. If it is 0,
  1255. * the module should assume that there is no message body to read.
  1256. *
  1257. * 3. Finally, call ap_get_client_block in a loop. Pass it a buffer and its size.
  1258. * It will put data into the buffer (not necessarily a full buffer), and
  1259. * return the length of the input block. When it is done reading, it will
  1260. * return 0 if EOF, or -1 if there was an error.
  1261. * If an error occurs on input, we force an end to keepalive.
  1262. *
  1263. * This step also sends a 100 Continue response to HTTP/1.1 clients if appropriate.
  1264. */
  1265. AP_DECLARE(int) ap_setup_client_block(request_rec *r, int read_policy)
  1266. {
  1267. const char *tenc = apr_table_get(r->headers_in, "Transfer-Encoding");
  1268. const char *lenp = apr_table_get(r->headers_in, "Content-Length");
  1269. r->read_body = read_policy;
  1270. r->read_chunked = 0;
  1271. r->remaining = 0;
  1272. if (tenc) {
  1273. if (strcasecmp(tenc, "chunked")) {
  1274. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
  1275. "Unknown Transfer-Encoding %s", tenc);
  1276. return HTTP_NOT_IMPLEMENTED;
  1277. }
  1278. if (r->read_body == REQUEST_CHUNKED_ERROR) {
  1279. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
  1280. "chunked Transfer-Encoding forbidden: %s", r->uri);
  1281. return (lenp) ? HTTP_BAD_REQUEST : HTTP_LENGTH_REQUIRED;
  1282. }
  1283. r->read_chunked = 1;
  1284. }
  1285. else if (lenp) {
  1286. char *endstr;
  1287. if (apr_strtoff(&r->remaining, lenp, &endstr, 10)
  1288. || *endstr || r->remaining < 0) {
  1289. r->remaining = 0;
  1290. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
  1291. "Invalid Content-Length");
  1292. return HTTP_BAD_REQUEST;
  1293. }
  1294. }
  1295. if ((r->read_body == REQUEST_NO_BODY)
  1296. && (r->read_chunked || (r->remaining > 0))) {
  1297. ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r,
  1298. "%s with body is not allowed for %s", r->method, r->uri);
  1299. return HTTP_REQUEST_ENTITY_TOO_LARGE;
  1300. }
  1301. #ifdef AP_DEBUG
  1302. {
  1303. /* Make sure ap_getline() didn't leave any droppings. */
  1304. core_request_config *req_cfg =
  1305. (core_request_config *)ap_get_module_config(r->request_config,
  1306. &core_module);
  1307. AP_DEBUG_ASSERT(APR_BRIGADE_EMPTY(req_cfg->bb));
  1308. }
  1309. #endif
  1310. return OK;
  1311. }
  1312. AP_DECLARE(int) ap_should_client_block(request_rec *r)
  1313. {
  1314. /* First check if we have already read the request body */
  1315. if (r->read_length || (!r->read_chunked && (r->remaining <= 0))) {
  1316. return 0;
  1317. }
  1318. return 1;
  1319. }
  1320. /* get_client_block is called in a loop to get the request message body.
  1321. * This is quite simple if the client includes a content-length
  1322. * (the normal case), but gets messy if the body is chunked. Note that
  1323. * r->remaining is used to maintain state across calls and that
  1324. * r->read_length is the total number of bytes given to the caller
  1325. * across all invocations. It is messy because we have to be careful not
  1326. * to read past the data provided by the client, since these reads block.
  1327. * Returns 0 on End-of-body, -1 on error or premature chunk end.
  1328. *
  1329. */
  1330. AP_DECLARE(long) ap_get_client_block(request_rec *r, char *buffer,
  1331. apr_size_t bufsiz)
  1332. {
  1333. apr_status_t rv;
  1334. apr_bucket_brigade *bb;
  1335. if (r->remaining < 0 || (!r->read_chunked && r->remaining == 0)) {
  1336. return 0;
  1337. }
  1338. bb = apr_brigade_create(r->pool, r->connection->bucket_alloc);
  1339. if (bb == NULL) {
  1340. r->connection->keepalive = AP_CONN_CLOSE;
  1341. return -1;
  1342. }
  1343. rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES,
  1344. APR_BLOCK_READ, bufsiz);
  1345. /* We lose the failure code here. This is why ap_get_client_block should
  1346. * not be used.
  1347. */
  1348. if (rv != APR_SUCCESS) {
  1349. /* if we actually fail here, we want to just return and
  1350. * stop trying to read data from the client.
  1351. */
  1352. r->connection->keepalive = AP_CONN_CLOSE;
  1353. apr_brigade_destroy(bb);
  1354. return -1;
  1355. }
  1356. /* If this fails, it means that a filter is written incorrectly and that
  1357. * it needs to learn how to properly handle APR_BLOCK_READ requests by
  1358. * returning data when requested.
  1359. */
  1360. AP_DEBUG_ASSERT(!APR_BRIGADE_EMPTY(bb));
  1361. /* Check to see if EOS in the brigade.
  1362. *
  1363. * If so, we have to leave a nugget for the *next* ap_get_client_block
  1364. * call to return 0.
  1365. */
  1366. if (APR_BUCKET_IS_EOS(APR_BRIGADE_LAST(bb))) {
  1367. if (r->read_chunked) {
  1368. r->remaining = -1;
  1369. }
  1370. else {
  1371. r->remaining = 0;
  1372. }
  1373. }
  1374. rv = apr_brigade_flatten(bb, buffer, &bufsiz);
  1375. if (rv != APR_SUCCESS) {
  1376. apr_brigade_destroy(bb);
  1377. return -1;
  1378. }
  1379. /* XXX yank me? */
  1380. r->read_length += bufsiz;
  1381. apr_brigade_destroy(bb);
  1382. return bufsiz;
  1383. }
  1384. /* Context struct for ap_http_outerror_filter */
  1385. typedef struct {
  1386. int seen_eoc;
  1387. } outerror_filter_ctx_t;
  1388. /* Filter to handle any error buckets on output */
  1389. apr_status_t ap_http_outerror_filter(ap_filter_t *f,
  1390. apr_bucket_brigade *b)
  1391. {
  1392. request_rec *r = f->r;
  1393. outerror_filter_ctx_t *ctx = (outerror_filter_ctx_t *)(f->ctx);
  1394. apr_bucket *e;
  1395. /* Create context if none is present */
  1396. if (!ctx) {
  1397. ctx = apr_pcalloc(r->pool, sizeof(outerror_filter_ctx_t));
  1398. f->ctx = ctx;
  1399. }
  1400. for (e = APR_BRIGADE_FIRST(b);
  1401. e != APR_BRIGADE_SENTINEL(b);
  1402. e = APR_BUCKET_NEXT(e))
  1403. {
  1404. if (AP_BUCKET_IS_ERROR(e)) {
  1405. /*
  1406. * Start of error handling state tree. Just one condition
  1407. * right now :)
  1408. */
  1409. if (((ap_bucket_error *)(e->data))->status == HTTP_BAD_GATEWAY) {
  1410. /* stream aborted and we have not ended it yet */
  1411. r->connection->keepalive = AP_CONN_CLOSE;
  1412. }
  1413. continue;
  1414. }
  1415. /* Detect EOC buckets and memorize this in the context. */
  1416. if (AP_BUCKET_IS_EOC(e)) {
  1417. ctx->seen_eoc = 1;
  1418. }
  1419. }
  1420. /*
  1421. * Remove all data buckets that are in a brigade after an EOC bucket
  1422. * was seen, as an EOC bucket tells us that no (further) resource
  1423. * and protocol data should go out to the client. OTOH meta buckets
  1424. * are still welcome as they might trigger needed actions down in
  1425. * the chain (e.g. in network filters like SSL).
  1426. * Remark 1: It is needed to dump ALL data buckets in the brigade
  1427. * since an filter in between might have inserted data
  1428. * buckets BEFORE the EOC bucket sent by the original
  1429. * sender and we do NOT want this data to be sent.
  1430. * Remark 2: Dumping all data buckets here does not necessarily mean
  1431. * that no further data is send to the client as:
  1432. * 1. Network filters like SSL can still be triggered via
  1433. * meta buckets to talk with the client e.g. for a
  1434. * clean shutdown.
  1435. * 2. There could be still data that was buffered before
  1436. * down in the chain that gets flushed by a FLUSH or an
  1437. * EOS bucket.
  1438. */
  1439. if (ctx->seen_eoc) {
  1440. for (e = APR_BRIGADE_FIRST(b);
  1441. e != APR_BRIGADE_SENTINEL(b);
  1442. e = APR_BUCKET_NEXT(e))
  1443. {
  1444. if (!APR_BUCKET_IS_METADATA(e)) {
  1445. APR_BUCKET_REMOVE(e);
  1446. }
  1447. }
  1448. }
  1449. return ap_pass_brigade(f->next, b);
  1450. }