/
http-server-connection.c
1278 lines (1083 loc) · 34.9 KB
/
http-server-connection.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "llist.h"
#include "array.h"
#include "str.h"
#include "ioloop.h"
#include "istream.h"
#include "istream-timeout.h"
#include "ostream.h"
#include "connection.h"
#include "iostream-rawlog.h"
#include "iostream-ssl.h"
#include "master-service.h"
#include "master-service-ssl.h"
#include "http-date.h"
#include "http-request-parser.h"
#include "http-server-private.h"
static void
http_server_connection_disconnect(struct http_server_connection *conn,
const char *reason);
static bool
http_server_connection_unref_is_closed(struct http_server_connection *conn);
/*
* Logging
*/
static inline void
http_server_connection_debug(struct http_server_connection *conn,
const char *format, ...) ATTR_FORMAT(2, 3);
static inline void
http_server_connection_debug(struct http_server_connection *conn,
const char *format, ...)
{
va_list args;
if (conn->server->set.debug) {
va_start(args, format);
i_debug("http-server: conn %s: %s",
http_server_connection_label(conn), t_strdup_vprintf(format, args));
va_end(args);
}
}
static inline void
http_server_connection_error(struct http_server_connection *conn,
const char *format, ...) ATTR_FORMAT(2, 3);
static inline void
http_server_connection_error(struct http_server_connection *conn,
const char *format, ...)
{
va_list args;
va_start(args, format);
i_error("http-server: conn %s: %s",
http_server_connection_label(conn), t_strdup_vprintf(format, args));
va_end(args);
}
static inline void
http_server_connection_client_error(struct http_server_connection *conn,
const char *format, ...) ATTR_FORMAT(2, 3);
static inline void
http_server_connection_client_error(struct http_server_connection *conn,
const char *format, ...)
{
va_list args;
va_start(args, format);
i_info("http-server: conn %s: %s",
http_server_connection_label(conn), t_strdup_vprintf(format, args));
va_end(args);
}
/*
* Connection
*/
static void http_server_connection_input(struct connection *_conn);
static void
http_server_connection_update_stats(struct http_server_connection *conn)
{
if (conn->conn.input != NULL)
conn->stats.input = conn->conn.input->v_offset;
if (conn->conn.output != NULL)
conn->stats.output = conn->conn.output->offset;
}
const struct http_server_stats *
http_server_connection_get_stats(struct http_server_connection *conn)
{
http_server_connection_update_stats(conn);
return &conn->stats;
}
static void
http_server_connection_input_halt(struct http_server_connection *conn)
{
connection_input_halt(&conn->conn);
}
static void
http_server_connection_input_resume(struct http_server_connection *conn)
{
if (!conn->closed && !conn->input_broken && !conn->close_indicated &&
!conn->in_req_callback && conn->incoming_payload == NULL) {
connection_input_resume(&conn->conn);
}
}
static void
http_server_connection_idle_timeout(struct http_server_connection *conn)
{
http_server_connection_client_error(conn, "Disconnected for inactivity");
http_server_connection_close(&conn, "Disconnected for inactivity");
}
static void
http_server_connection_timeout_stop(struct http_server_connection *conn)
{
timeout_remove(&conn->to_idle);
}
static void
http_server_connection_timeout_start(struct http_server_connection *conn)
{
if (conn->to_idle == NULL && conn->server->set.max_client_idle_time_msecs > 0) {
conn->to_idle = timeout_add(conn->server->set.max_client_idle_time_msecs,
http_server_connection_idle_timeout, conn);
}
}
static void
http_server_connection_timeout_reset(struct http_server_connection *conn)
{
if (conn->to_idle != NULL)
timeout_reset(conn->to_idle);
}
bool http_server_connection_shut_down(struct http_server_connection *conn)
{
if (conn->request_queue_head == NULL ||
conn->request_queue_head->state == HTTP_SERVER_REQUEST_STATE_NEW) {
http_server_connection_close(&conn, "Server shutting down");
return TRUE;
}
return FALSE;
}
static void http_server_connection_ready(struct http_server_connection *conn)
{
struct stat st;
if (conn->server->set.rawlog_dir != NULL &&
stat(conn->server->set.rawlog_dir, &st) == 0) {
iostream_rawlog_create(conn->server->set.rawlog_dir,
&conn->conn.input, &conn->conn.output);
}
conn->http_parser = http_request_parser_init
(conn->conn.input, &conn->server->set.request_limits,
HTTP_REQUEST_PARSE_FLAG_STRICT);
o_stream_set_flush_callback(conn->conn.output,
http_server_connection_output, conn);
}
static void http_server_connection_destroy(struct connection *_conn)
{
struct http_server_connection *conn =
(struct http_server_connection *)_conn;
http_server_connection_disconnect(conn, NULL);
http_server_connection_unref(&conn);
}
static void http_server_payload_finished(struct http_server_connection *conn)
{
timeout_remove(&conn->to_input);
http_server_connection_input_resume(conn);
}
static void
http_server_payload_destroyed_timeout(struct http_server_connection *conn)
{
http_server_connection_input(&conn->conn);
}
static void http_server_payload_destroyed(struct http_server_request *req)
{
struct http_server_connection *conn = req->conn;
int stream_errno;
i_assert(conn != NULL);
i_assert(conn->request_queue_tail == req ||
req->state >= HTTP_SERVER_REQUEST_STATE_FINISHED);
i_assert(conn->conn.io == NULL);
http_server_connection_debug(conn, "Request payload stream destroyed");
/* caller is allowed to change the socket fd to blocking while reading
the payload. make sure here that it's switched back. */
net_set_nonblock(conn->conn.fd_in, TRUE);
stream_errno = conn->incoming_payload->stream_errno;
conn->incoming_payload = NULL;
if (conn->payload_handler != NULL)
http_server_payload_handler_destroy(&conn->payload_handler);
/* handle errors in transfer stream */
if (req->response == NULL && stream_errno != 0 &&
conn->conn.input->stream_errno == 0) {
switch (stream_errno) {
case EMSGSIZE:
conn->input_broken = TRUE;
http_server_connection_client_error(conn,
"Client sent excessively large request");
http_server_request_fail_close(req, 413, "Payload Too Large");
return;
case EIO:
conn->input_broken = TRUE;
http_server_connection_client_error(conn,
"Client sent invalid request payload");
http_server_request_fail_close(req, 400, "Bad Request");
return;
default:
break;
}
}
/* resource stopped reading payload; update state */
switch (req->state) {
case HTTP_SERVER_REQUEST_STATE_QUEUED:
case HTTP_SERVER_REQUEST_STATE_PAYLOAD_IN:
/* finished reading request */
req->state = HTTP_SERVER_REQUEST_STATE_PROCESSING;
http_server_connection_timeout_stop(conn);
if (req->response != NULL && req->response->submitted)
http_server_request_submit_response(req);
break;
case HTTP_SERVER_REQUEST_STATE_PROCESSING:
/* no response submitted yet */
break;
case HTTP_SERVER_REQUEST_STATE_SUBMITTED_RESPONSE:
/* response submitted, but not all payload is necessarily read */
if (http_server_request_is_complete(req))
http_server_request_ready_to_respond(req);
break;
case HTTP_SERVER_REQUEST_STATE_READY_TO_RESPOND:
case HTTP_SERVER_REQUEST_STATE_FINISHED:
case HTTP_SERVER_REQUEST_STATE_ABORTED:
/* nothing to do */
break;
default:
i_unreached();
}
/* input stream may have pending input. make sure input handler
gets called (but don't do it directly, since we get get here
somewhere from the API user's code, which we can't really know what
state it is in). this call also triggers sending the next response if
necessary. */
if (!conn->input_broken && !conn->in_req_callback) {
conn->to_input =
timeout_add_short(0, http_server_payload_destroyed_timeout, conn);
}
}
static void http_server_connection_request_callback(
struct http_server_connection *conn, struct http_server_request *req)
{
/* CONNECT method */
if (strcmp(req->req.method, "CONNECT") == 0) {
if (conn->callbacks->handle_connect_request == NULL) {
http_server_request_fail(req, 505, "Not Implemented");
return;
}
if (req->req.target.format != HTTP_REQUEST_TARGET_FORMAT_AUTHORITY) {
http_server_request_fail(req, 400, "Bad Request");
return;
}
conn->callbacks->handle_connect_request
(conn->context, req, req->req.target.url);
/* other methods */
} else {
if (conn->callbacks->handle_request == NULL) {
http_server_request_fail(req, 505, "Not Implemented");
return;
}
conn->callbacks->handle_request(conn->context, req);
}
}
static bool
http_server_connection_handle_request(struct http_server_connection *conn,
struct http_server_request *req)
{
const struct http_server_settings *set = &conn->server->set;
unsigned int old_refcount;
struct istream *payload;
bool payload_destroyed = FALSE;
i_assert(!conn->in_req_callback);
i_assert(conn->incoming_payload == NULL);
if (req->req.version_major != 1) {
http_server_request_fail(req, 505, "HTTP Version Not Supported");
return TRUE;
}
req->state = HTTP_SERVER_REQUEST_STATE_QUEUED;
if (req->req.payload != NULL) {
/* wrap the stream to capture the destroy event without destroying the
actual payload stream. */
conn->incoming_payload = req->req.payload =
i_stream_create_timeout(req->req.payload,
set->max_client_idle_time_msecs);
/* we've received the request itself, and we can't reset the
timeout during the payload reading. */
http_server_connection_timeout_stop(conn);
} else {
conn->incoming_payload = req->req.payload =
i_stream_create_from_data("", 0);
}
i_stream_add_destroy_callback(req->req.payload,
http_server_payload_destroyed, req);
/* the callback may add its own I/O, so we need to remove
our one before calling it */
http_server_connection_input_halt(conn);
old_refcount = req->refcount;
conn->in_req_callback = TRUE;
http_server_connection_request_callback(conn, req);
if (conn->closed) {
/* the callback managed to get this connection destroyed/closed */
return FALSE;
}
conn->in_req_callback = FALSE;
req->callback_refcount = req->refcount - old_refcount;
if (req->req.payload != NULL) {
/* send 100 Continue when appropriate */
if (req->req.expect_100_continue && !req->payload_halted
&& req->response == NULL) {
http_server_connection_trigger_responses(conn);
}
/* delegate payload handling to request handler */
if (req->state < HTTP_SERVER_REQUEST_STATE_PAYLOAD_IN)
req->state = HTTP_SERVER_REQUEST_STATE_PAYLOAD_IN;
payload = req->req.payload;
req->req.payload = NULL;
i_stream_unref(&payload);
if (conn->to_input != NULL) {
/* already finished reading the payload */
http_server_payload_finished(conn);
payload_destroyed = TRUE;
}
}
if (req->state < HTTP_SERVER_REQUEST_STATE_PROCESSING &&
(conn->incoming_payload == NULL ||
!i_stream_have_bytes_left(conn->incoming_payload))) {
/* finished reading request */
req->state = HTTP_SERVER_REQUEST_STATE_PROCESSING;
if (req->response != NULL && req->response->submitted)
http_server_request_submit_response(req);
}
i_assert(!payload_destroyed || req->callback_refcount > 0 ||
(req->response != NULL && req->response->submitted));
if (conn->incoming_payload == NULL) {
if (conn->conn.io == NULL && conn->to_input == NULL)
http_server_connection_input_resume(conn);
return TRUE;
}
/* Request payload is still being uploaded by the client */
return FALSE;
}
static int
http_server_connection_ssl_init(struct http_server_connection *conn)
{
struct http_server *server = conn->server;
const char *error;
int ret;
if (http_server_init_ssl_ctx(server, &error) < 0) {
http_server_connection_error(conn,
"Couldn't initialize SSL: %s", error);
return -1;
}
if (server->set.debug)
http_server_connection_debug(conn, "Starting SSL handshake");
http_server_connection_input_halt(conn);
if (server->ssl_ctx == NULL) {
ret = master_service_ssl_init(master_service,
&conn->conn.input, &conn->conn.output,
&conn->ssl_iostream, &error);
} else {
ret = io_stream_create_ssl_server(server->ssl_ctx,
server->set.ssl, &conn->conn.input, &conn->conn.output,
&conn->ssl_iostream, &error);
}
if (ret < 0) {
http_server_connection_error(conn,
"Couldn't initialize SSL server for %s: %s",
conn->conn.name, error);
return -1;
}
http_server_connection_input_resume(conn);
if (ssl_iostream_handshake(conn->ssl_iostream) < 0) {
http_server_connection_error(conn,"SSL handshake failed: %s",
ssl_iostream_get_last_error(conn->ssl_iostream));
return -1;
}
http_server_connection_ready(conn);
return 0;
}
static bool
http_server_connection_pipeline_is_full(struct http_server_connection *conn)
{
return (conn->request_queue_count >=
conn->server->set.max_pipelined_requests ||
conn->server->shutting_down);
}
static void
http_server_connection_pipeline_handle_full(
struct http_server_connection *conn)
{
if (conn->server->shutting_down) {
http_server_connection_debug(conn,
"Pipeline full (%u requests pending; server shutting down)",
conn->request_queue_count);
} else {
http_server_connection_debug(conn,
"Pipeline full (%u requests pending; %u maximum)",
conn->request_queue_count,
conn->server->set.max_pipelined_requests);
}
http_server_connection_input_halt(conn);
}
static bool
http_server_connection_check_input(struct http_server_connection *conn)
{
struct istream *input = conn->conn.input;
int stream_errno;
if (input == NULL)
return FALSE;
stream_errno = input->stream_errno;
if (input->eof || stream_errno != 0) {
/* connection input broken; output may still be intact */
if (stream_errno != 0 && stream_errno != EPIPE &&
stream_errno != ECONNRESET) {
http_server_connection_client_error(conn,
"Connection lost: read(%s) failed: %s",
i_stream_get_name(input),
i_stream_get_error(input));
http_server_connection_close(&conn, "Read failure");
} else {
http_server_connection_debug(conn,
"Connection lost: Remote disconnected");
if (conn->request_queue_head == NULL) {
/* no pending requests; close */
http_server_connection_close(&conn,
"Remote closed connection");
} else if (conn->request_queue_head->state <
HTTP_SERVER_REQUEST_STATE_SUBMITTED_RESPONSE) {
/* unfinished request; close */
http_server_connection_close(&conn,
"Remote closed connection unexpectedly");
} else {
/* a request is still processing; only drop input io for now.
the other end may only have shutdown one direction */
conn->input_broken = TRUE;
http_server_connection_input_halt(conn);
}
}
return FALSE;
}
return TRUE;
}
static bool
http_server_connection_finish_request(struct http_server_connection *conn)
{
struct http_server_request *req;
enum http_request_parse_error error_code;
const char *error;
int ret;
req = conn->request_queue_tail;
if (req != NULL &&
req->state == HTTP_SERVER_REQUEST_STATE_SUBMITTED_RESPONSE) {
http_server_connection_debug(conn, "Finish receiving request");
ret = http_request_parse_finish_payload
(conn->http_parser, &error_code, &error);
if (ret <= 0 &&
!http_server_connection_check_input(conn))
return FALSE;
if (ret < 0) {
http_server_connection_ref(conn);
http_server_connection_client_error(conn,
"Client sent invalid request: %s", error);
switch (error_code) {
case HTTP_REQUEST_PARSE_ERROR_PAYLOAD_TOO_LARGE:
conn->input_broken = TRUE;
http_server_request_fail_close(req,
413, "Payload Too Large");
break;
case HTTP_REQUEST_PARSE_ERROR_BROKEN_REQUEST:
conn->input_broken = TRUE;
http_server_request_fail_close(req,
400, "Bad request");
break;
default:
i_unreached();
}
if (http_server_connection_unref_is_closed(conn)) {
/* connection got closed */
return FALSE;
}
if (conn->input_broken || conn->close_indicated)
http_server_connection_input_halt(conn);
return FALSE;
}
if (ret == 0)
return FALSE;
http_server_request_ready_to_respond(req);
}
return TRUE;
}
static void http_server_connection_input(struct connection *_conn)
{
struct http_server_connection *conn =
(struct http_server_connection *)_conn;
struct http_server_request *req;
enum http_request_parse_error error_code;
const char *error;
bool cont;
int ret;
if (conn->server->shutting_down) {
if (!http_server_connection_shut_down(conn))
http_server_connection_pipeline_handle_full(conn);
return;
}
i_assert(!conn->in_req_callback);
i_assert(!conn->input_broken && conn->incoming_payload == NULL);
i_assert(!conn->close_indicated);
http_server_connection_timeout_reset(conn);
if (conn->ssl && conn->ssl_iostream == NULL) {
if (http_server_connection_ssl_init(conn) < 0) {
/* ssl failed */
http_server_connection_close(&conn, "SSL Initialization failed");
return;
}
}
if (conn->to_input != NULL) {
/* We came here from a timeout added by
http_server_payload_destroyed(). The IO couldn't be added
back immediately in there, because the HTTP API user may
still have had its own IO pointed to the same fd. It should
be removed by now, so we can add it back. */
http_server_payload_finished(conn);
}
/* finish up pending request */
if (!http_server_connection_finish_request(conn))
return;
/* create request object if none was created already */
if (conn->request_queue_tail != NULL &&
conn->request_queue_tail->state == HTTP_SERVER_REQUEST_STATE_NEW) {
if (conn->request_queue_count >
conn->server->set.max_pipelined_requests) {
/* pipeline full */
http_server_connection_pipeline_handle_full(conn);
return;
}
/* continue last unfinished request*/
req = conn->request_queue_tail;
} else {
if (conn->request_queue_count >=
conn->server->set.max_pipelined_requests) {
/* pipeline full */
http_server_connection_pipeline_handle_full(conn);
return;
}
/* start new request */
req = http_server_request_new(conn);
}
/* parse requests */
ret = 1;
while (!conn->close_indicated && ret != 0) {
http_server_connection_ref(conn);
while ((ret = http_request_parse_next (conn->http_parser,
req->pool, &req->req, &error_code, &error)) > 0) {
conn->stats.request_count++;
http_server_connection_debug(conn,
"Received new request %s "
"(%u requests pending; %u maximum)",
http_server_request_label(req),
conn->request_queue_count,
conn->server->set.max_pipelined_requests);
http_server_request_ref(req);
i_assert(!req->delay_destroy);
req->delay_destroy = TRUE;
T_BEGIN {
cont = http_server_connection_handle_request(conn, req);
} T_END;
req->delay_destroy = FALSE;
if (!cont) {
/* connection closed or request body not read yet.
the request may be destroyed now. */
if (req->destroy_pending)
http_server_request_destroy(&req);
else
http_server_request_unref(&req);
http_server_connection_unref(&conn);
return;
}
if (req->req.connection_close)
conn->close_indicated = TRUE;
if (req->destroy_pending)
http_server_request_destroy(&req);
else
http_server_request_unref(&req);
if (conn->closed) {
/* connection got closed in destroy callback */
break;
}
if (conn->close_indicated) {
/* client indicated it will close after this request; stop trying
to read more. */
break;
}
/* finish up pending request if possible */
if (!http_server_connection_finish_request(conn)) {
http_server_connection_unref(&conn);
return;
}
if (http_server_connection_pipeline_is_full(conn)) {
/* pipeline full */
http_server_connection_pipeline_handle_full(conn);
http_server_connection_unref(&conn);
return;
}
/* start new request */
req = http_server_request_new(conn);
}
if (http_server_connection_unref_is_closed(conn)) {
/* connection got closed */
return;
}
if (ret <= 0 &&
!http_server_connection_check_input(conn))
return;
if (ret < 0) {
http_server_connection_ref(conn);
http_server_connection_client_error(conn,
"Client sent invalid request: %s", error);
switch (error_code) {
case HTTP_REQUEST_PARSE_ERROR_BROKEN_REQUEST:
conn->input_broken = TRUE;
/* fall through */
case HTTP_REQUEST_PARSE_ERROR_BAD_REQUEST:
http_server_request_fail(req, 400, "Bad Request");
break;
case HTTP_REQUEST_PARSE_ERROR_METHOD_TOO_LONG:
conn->input_broken = TRUE;
/* fall through */
case HTTP_REQUEST_PARSE_ERROR_NOT_IMPLEMENTED:
http_server_request_fail(req, 501, "Not Implemented");
break;
case HTTP_REQUEST_PARSE_ERROR_TARGET_TOO_LONG:
conn->input_broken = TRUE;
http_server_request_fail_close(req, 414, "URI Too Long");
break;
case HTTP_REQUEST_PARSE_ERROR_EXPECTATION_FAILED:
http_server_request_fail(req, 417, "Expectation Failed");
break;
case HTTP_REQUEST_PARSE_ERROR_PAYLOAD_TOO_LARGE:
conn->input_broken = TRUE;
http_server_request_fail_close(req, 413, "Payload Too Large");
break;
default:
i_unreached();
}
if (http_server_connection_unref_is_closed(conn)) {
/* connection got closed */
return;
}
}
if (conn->input_broken || conn->close_indicated) {
http_server_connection_input_halt(conn);
return;
}
}
}
static void
http_server_connection_discard_input(struct http_server_connection *conn)
{
struct http_server *server = conn->server;
enum http_request_parse_error error_code;
const char *error;
int ret = 0;
i_assert(server->ioloop != NULL);
ret = http_request_parse_finish_payload
(conn->http_parser, &error_code, &error);
if (ret <= 0 &&
!http_server_connection_check_input(conn)) {
io_loop_stop(server->ioloop);
return;
}
if (ret < 0) {
http_server_connection_ref(conn);
http_server_connection_client_error(conn,
"Client sent invalid request: %s", error);
switch (error_code) {
case HTTP_REQUEST_PARSE_ERROR_PAYLOAD_TOO_LARGE:
conn->input_broken = TRUE;
http_server_request_fail_close(conn->request_queue_head,
413, "Payload Too Large");
break;
default:
i_unreached();
}
http_server_connection_unref(&conn);
io_loop_stop(server->ioloop);
return;
}
if (ret > 0)
io_loop_stop(server->ioloop);
}
int http_server_connection_discard_payload(
struct http_server_connection *conn)
{
struct http_server *server = conn->server;
struct ioloop *prev_ioloop = current_ioloop;
i_assert(conn->conn.io == NULL);
i_assert(server->ioloop == NULL);
if (conn->payload_handler != NULL)
http_server_payload_handler_destroy(&conn->payload_handler);
/* destroy payload wrapper early to advance state */
if (conn->incoming_payload != NULL) {
i_stream_unref(&conn->incoming_payload);
conn->incoming_payload = NULL;
}
/* finish reading payload from the parser */
if (http_request_parser_pending_payload
(conn->http_parser)) {
http_server_connection_debug(conn,
"Discarding remaining incoming payload");
server->ioloop = io_loop_create();
http_server_connection_switch_ioloop(conn);
io_loop_set_running(server->ioloop);
conn->conn.io = io_add_istream(conn->conn.input,
http_server_connection_discard_input, conn);
http_server_connection_discard_input(conn);
if (io_loop_is_running(server->ioloop))
io_loop_run(server->ioloop);
io_remove(&conn->conn.io);
io_loop_set_current(prev_ioloop);
http_server_connection_switch_ioloop(conn);
io_loop_set_current(server->ioloop);
io_loop_destroy(&server->ioloop);
} else {
http_server_connection_debug(conn,
"No remaining incoming payload");
}
/* check whether connection is still viable */
http_server_connection_ref(conn);
(void)http_server_connection_check_input(conn);
return http_server_connection_unref_is_closed(conn) ? -1 : 0;
}
void http_server_connection_handle_output_error(
struct http_server_connection *conn)
{
struct ostream *output = conn->conn.output;
if (conn->closed)
return;
if (output->stream_errno != EPIPE &&
output->stream_errno != ECONNRESET) {
http_server_connection_error(conn,
"Connection lost: write(%s) failed: %s",
o_stream_get_name(output),
o_stream_get_error(output));
http_server_connection_close(&conn,
"Write failure");
} else {
http_server_connection_debug(conn,
"Connection lost: Remote disconnected");
http_server_connection_close(&conn,
"Remote closed connection unexpectedly");
}
}
static bool
http_server_connection_next_response(struct http_server_connection *conn)
{
struct http_server_request *req;
int ret;
if (conn->output_locked)
return FALSE;
req = conn->request_queue_head;
if (req == NULL || req->state == HTTP_SERVER_REQUEST_STATE_NEW) {
/* no requests pending */
http_server_connection_debug(conn, "No more requests pending");
http_server_connection_timeout_start(conn);
return FALSE;
}
if (req->state < HTTP_SERVER_REQUEST_STATE_READY_TO_RESPOND) {
if (req->state == HTTP_SERVER_REQUEST_STATE_PROCESSING) {
/* server is causing idle time */
http_server_connection_debug(conn,
"Not ready to respond: Server is processing");
http_server_connection_timeout_stop(conn);
} else {
/* client is causing idle time */
http_server_connection_debug(conn,
"Not ready to respond: Waiting for client");
http_server_connection_timeout_start(conn);
}
/* send 100 Continue if appropriate */
if (req->state >= HTTP_SERVER_REQUEST_STATE_QUEUED
&& conn->incoming_payload != NULL
&& req->response == NULL && req->req.version_minor >= 1
&& req->req.expect_100_continue && !req->payload_halted
&& !req->sent_100_continue) {
static const char *response = "HTTP/1.1 100 Continue\r\n\r\n";
struct ostream *output = conn->conn.output;
if (o_stream_send(output, response, strlen(response)) < 0) {
http_server_connection_handle_output_error(conn);
return FALSE;
}
http_server_connection_debug(conn, "Sent 100 Continue");
req->sent_100_continue = TRUE;
}
return FALSE;
}
i_assert(req->state == HTTP_SERVER_REQUEST_STATE_READY_TO_RESPOND &&
req->response != NULL);
http_server_connection_debug(conn,
"Sending response");
http_server_connection_timeout_start(conn);
http_server_request_ref(req);
ret = http_server_response_send(req->response);
http_server_request_unref(&req);
if (ret < 0)
return FALSE;
http_server_connection_timeout_reset(conn);
return TRUE;
}
static int http_server_connection_send_responses(
struct http_server_connection *conn)
{
http_server_connection_ref(conn);
/* send more responses until no more responses remain, the output
blocks again, or the connection is closed */
while (!conn->closed && http_server_connection_next_response(conn));
if (http_server_connection_unref_is_closed(conn))
return -1;
/* accept more requests if possible */
if (conn->incoming_payload == NULL &&
conn->request_queue_count < conn->server->set.max_pipelined_requests &&
!conn->server->shutting_down) {
http_server_connection_input_resume(conn);
return 1;
}
return 0;
}
int http_server_connection_flush(struct http_server_connection *conn)
{
struct ostream *output = conn->conn.output;
int ret;
if ((ret = o_stream_flush(output)) <= 0) {
if (ret < 0)
http_server_connection_handle_output_error(conn);
return -1;
}
http_server_connection_timeout_reset(conn);
return 0;
}
int http_server_connection_output(struct http_server_connection *conn)
{
bool pipeline_was_full =
http_server_connection_pipeline_is_full(conn);
int ret;
if (http_server_connection_flush(conn) < 0)
return -1;
if (!conn->output_locked) {
if (http_server_connection_send_responses(conn) < 0)
return -1;
} else if (conn->request_queue_head != NULL) {
struct http_server_request *req = conn->request_queue_head;
struct http_server_response *resp = req->response;
http_server_connection_ref(conn);
i_assert(resp != NULL);
ret = http_server_response_send_more(resp);
if (http_server_connection_unref_is_closed(conn) || ret < 0)
return -1;
if (!conn->output_locked) {