/
http-client-connection.c
1821 lines (1543 loc) · 52.4 KB
/
http-client-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 "net.h"
#include "str.h"
#include "hash.h"
#include "llist.h"
#include "array.h"
#include "ioloop.h"
#include "istream.h"
#include "istream-timeout.h"
#include "ostream.h"
#include "time-util.h"
#include "file-lock.h"
#include "iostream-rawlog.h"
#include "iostream-ssl.h"
#include "http-response-parser.h"
#include "http-client-private.h"
/*
* Connection
*/
static void http_client_connection_ready(struct http_client_connection *conn);
static void http_client_connection_input(struct connection *_conn);
static inline void
http_client_connection_ref_request(struct http_client_connection *conn,
struct http_client_request *req)
{
i_assert(req->conn == NULL);
req->conn = conn;
http_client_request_ref(req);
}
static inline bool
http_client_connection_unref_request(struct http_client_connection *conn,
struct http_client_request **_req)
{
struct http_client_request *req = *_req;
i_assert(req->conn == conn);
req->conn = NULL;
return http_client_request_unref(_req);
}
static inline void
http_client_connection_failure(struct http_client_connection *conn,
const char *reason)
{
struct http_client_peer *peer = conn->peer;
conn->connect_failed = TRUE;
http_client_peer_connection_failure(peer, reason);
}
unsigned int
http_client_connection_count_pending(struct http_client_connection *conn)
{
unsigned int pending_count = array_count(&conn->request_wait_list);
if (conn->in_req_callback || conn->pending_request != NULL)
pending_count++;
return pending_count;
}
bool http_client_connection_is_idle(struct http_client_connection *conn)
{
return (conn->to_idle != NULL);
}
bool http_client_connection_is_active(struct http_client_connection *conn)
{
if (!conn->connected)
return FALSE;
if (conn->in_req_callback || conn->pending_request != NULL)
return TRUE;
return (array_is_created(&conn->request_wait_list) &&
array_count(&conn->request_wait_list) > 0);
}
static void
http_client_connection_retry_requests(struct http_client_connection *conn,
unsigned int status, const char *error)
{
const struct http_client_settings *set = &conn->peer->client->set;
struct http_client_request *req, **req_idx;
if (!array_is_created(&conn->request_wait_list))
return;
if (set->no_auto_retry) {
e_debug(conn->event, "Aborting pending requests with error");
} else {
e_debug(conn->event, "Retrying pending requests");
}
array_foreach_modifiable(&conn->request_wait_list, req_idx) {
req = *req_idx;
/* drop reference from connection */
if (!http_client_connection_unref_request(conn, req_idx))
continue;
/* retry the request, which may drop it */
if (req->state < HTTP_REQUEST_STATE_FINISHED) {
if (set->no_auto_retry)
http_client_request_error(&req, status, error);
else
http_client_request_retry(req, status, error);
}
}
array_clear(&conn->request_wait_list);
}
static void
http_client_connection_server_close(struct http_client_connection **_conn)
{
struct http_client_connection *conn = *_conn;
struct http_client_peer *peer = conn->peer;
struct http_client *client = peer->client;
struct http_client_request *req, **req_idx;
e_debug(conn->event, "Server explicitly closed connection");
array_foreach_modifiable(&conn->request_wait_list, req_idx) {
req = *req_idx;
/* drop reference from connection */
if (!http_client_connection_unref_request(conn, req_idx))
continue;
/* resubmit the request, which may drop it */
if (req->state < HTTP_REQUEST_STATE_FINISHED)
http_client_request_resubmit(req);
}
array_clear(&conn->request_wait_list);
if (client != NULL && client->waiting)
io_loop_stop(client->ioloop);
http_client_connection_close(_conn);
}
static void
http_client_connection_abort_error(struct http_client_connection **_conn,
unsigned int status, const char *error)
{
struct http_client_connection *conn = *_conn;
struct http_client_request *req, **req_idx;
e_debug(conn->event, "Aborting connection: %s", error);
array_foreach_modifiable(&conn->request_wait_list, req_idx) {
req = *req_idx;
i_assert(req->submitted);
/* drop reference from connection */
if (!http_client_connection_unref_request(conn, req_idx))
continue;
/* drop request if not already aborted */
http_client_request_error(&req, status, error);
}
array_clear(&conn->request_wait_list);
http_client_connection_close(_conn);
}
static void
http_client_connection_abort_any_requests(struct http_client_connection *conn)
{
struct http_client_request *req, **req_idx;
if (array_is_created(&conn->request_wait_list)) {
array_foreach_modifiable(&conn->request_wait_list, req_idx) {
req = *req_idx;
i_assert(req->submitted);
/* drop reference from connection */
if (!http_client_connection_unref_request(conn, req_idx))
continue;
/* drop request if not already aborted */
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_ABORTED,
"Aborting");
}
array_clear(&conn->request_wait_list);
}
if (conn->pending_request != NULL) {
req = conn->pending_request;
/* drop reference from connection */
if (http_client_connection_unref_request(conn, &conn->pending_request)) {
/* drop request if not already aborted */
http_client_request_error(&req,
HTTP_CLIENT_REQUEST_ERROR_ABORTED,
"Aborting");
}
}
}
static const char *
http_client_connection_get_timing_info(struct http_client_connection *conn)
{
struct http_client_request *const *requestp;
unsigned int connected_msecs;
string_t *str = t_str_new(64);
if (array_count(&conn->request_wait_list) > 0) {
requestp = array_idx(&conn->request_wait_list, 0);
str_append(str, "Request ");
http_client_request_append_stats_text(*requestp, str);
} else {
str_append(str, "No requests");
if (conn->conn.last_input != 0) {
str_printfa(str, ", last input %d secs ago",
(int)(ioloop_time - conn->conn.last_input));
}
}
connected_msecs = timeval_diff_msecs(&ioloop_timeval, &conn->connected_timestamp);
str_printfa(str, ", connected %u.%03u secs ago",
connected_msecs/1000, connected_msecs%1000);
return str_c(str);
}
static void
http_client_connection_abort_temp_error(struct http_client_connection **_conn,
unsigned int status, const char *error)
{
struct http_client_connection *conn = *_conn;
error = t_strdup_printf("%s (%s)", error,
http_client_connection_get_timing_info(conn));
e_debug(conn->event,
"Aborting connection with temporary error: %s", error);
http_client_connection_retry_requests(conn, status, error);
http_client_connection_close(_conn);
}
static void
http_client_connection_lost(struct http_client_connection **_conn,
const char *error) ATTR_NULL(2)
{
struct http_client_connection *conn = *_conn;
const char *sslerr;
if (error == NULL)
error = "Connection lost";
else
error = t_strdup_printf("Connection lost: %s", error);
if (conn->ssl_iostream != NULL) {
sslerr = ssl_iostream_get_last_error(conn->ssl_iostream);
if (sslerr != NULL) {
error = t_strdup_printf("%s (last SSL error: %s)",
error, sslerr);
}
if (ssl_iostream_has_handshake_failed(conn->ssl_iostream)) {
/* this isn't really a "connection lost", but that we
don't trust the remote's SSL certificate. don't
retry. */
http_client_connection_abort_error(_conn,
HTTP_CLIENT_REQUEST_ERROR_BAD_RESPONSE, error);
return;
}
}
conn->lost_prematurely = (conn->conn.input != NULL &&
conn->conn.input->v_offset == 0 &&
i_stream_get_data_size(conn->conn.input) == 0);
http_client_connection_abort_temp_error(_conn,
HTTP_CLIENT_REQUEST_ERROR_CONNECTION_LOST, error);
}
int http_client_connection_check_ready(struct http_client_connection *conn)
{
const struct http_client_settings *set = &conn->peer->client->set;
int ret;
if (conn->in_req_callback) {
/* this can happen when a nested ioloop is created inside request
callback. we currently don't reuse connections that are occupied
this way, but theoretically we could, although that would add
quite a bit of complexity.
*/
return 0;
}
if (!conn->connected || conn->output_locked || conn->output_broken ||
conn->close_indicated || conn->tunneling ||
http_client_connection_count_pending(conn) >=
set->max_pipelined_requests)
return 0;
if (conn->last_ioloop != NULL && conn->last_ioloop != current_ioloop) {
conn->last_ioloop = current_ioloop;
/* Active ioloop is different from what we saw earlier;
we may have missed a disconnection event on this connection.
Verify status by reading from connection. */
if ((ret=i_stream_read(conn->conn.input)) == -1) {
int stream_errno = conn->conn.input->stream_errno;
i_assert(conn->conn.input->stream_errno != 0 || conn->conn.input->eof);
http_client_connection_lost(&conn,
t_strdup_printf("read(%s) failed: %s",
i_stream_get_name(conn->conn.input),
stream_errno != 0 ?
i_stream_get_error(conn->conn.input) :
"EOF"));
return -1;
}
/* we may have read some data */
if (i_stream_get_data_size(conn->conn.input) > 0)
i_stream_set_input_pending(conn->conn.input, TRUE);
}
return 1;
}
static void
http_client_connection_detach_peer(struct http_client_connection *conn)
{
struct http_client_peer *peer = conn->peer;
struct http_client_connection *const *conn_idx;
ARRAY_TYPE(http_client_connection) *conn_arr;
bool found = FALSE;
if (peer == NULL)
return;
http_client_peer_ref(peer);
conn_arr = &peer->conns;
array_foreach(conn_arr, conn_idx) {
if (*conn_idx == conn) {
array_delete(conn_arr, array_foreach_idx(conn_arr, conn_idx), 1);
found = TRUE;
break;
}
}
i_assert(found);
conn->peer = NULL;
e_debug(conn->event, "Detached peer");
if (conn->connect_succeeded)
http_client_peer_connection_lost(peer, conn->lost_prematurely);
http_client_peer_unref(&peer);
}
static void
http_client_connection_idle_timeout(struct http_client_connection *conn)
{
e_debug(conn->event, "Idle connection timed out");
/* cannot get here unless connection was established at some point */
i_assert(conn->connect_succeeded);
http_client_connection_close(&conn);
}
void http_client_connection_lost_peer(struct http_client_connection *conn)
{
struct http_client_peer *peer = conn->peer;
struct http_client *client = peer->client;
const struct http_client_settings *set = &client->set;
struct http_client_peer_pool *ppool = conn->ppool;
struct http_client_peer_shared *pshared = ppool->peer;
unsigned int timeout, count;
if (!conn->connected) {
http_client_connection_unref(&conn);
return;
}
i_assert(!conn->in_req_callback);
if (conn->to_idle == NULL) {
count = array_count(&ppool->conns);
i_assert(count > 0);
/* set timeout for this connection */
if (count > http_client_peer_shared_max_connections(pshared)) {
/* instant death for (urgent) connections above limit */
timeout = 0;
} else {
unsigned int idle_count = array_count(&ppool->idle_conns);
/* kill duplicate connections quicker;
linearly based on the number of connections */
i_assert(count >= idle_count + 1);
timeout = (set->max_parallel_connections - idle_count) *
(set->max_idle_time_msecs / set->max_parallel_connections);
}
e_debug(conn->event,
"Lost peer; going idle (timeout = %u msecs)",
timeout);
conn->to_idle = timeout_add_to(conn->conn.ioloop, timeout,
http_client_connection_idle_timeout, conn);
array_append(&ppool->idle_conns, &conn, 1);
} else {
e_debug(conn->event, "Lost peer; already idle");
}
http_client_connection_detach_peer(conn);
}
void http_client_connection_check_idle(struct http_client_connection *conn)
{
struct http_client_peer *peer;
struct http_client_peer_pool *ppool = conn->ppool;
struct http_client *client;
const struct http_client_settings *set;
unsigned int timeout, count;
peer = conn->peer;
if (peer == NULL) {
i_assert(conn->to_idle != NULL);
return;
}
if (conn->to_idle != NULL) {
/* timeout already set */
return;
}
client = peer->client;
set = &client->set;
if (conn->connected &&
array_is_created(&conn->request_wait_list) &&
array_count(&conn->request_wait_list) == 0 &&
!conn->in_req_callback &&
conn->incoming_payload == NULL &&
set->max_idle_time_msecs > 0) {
i_assert(peer != NULL);
client = peer->client;
if (client->waiting)
io_loop_stop(client->ioloop);
count = array_count(&peer->conns);
i_assert(count > 0);
/* set timeout for this connection */
if (count > set->max_parallel_connections) {
/* instant death for (urgent) connections above limit */
timeout = 0;
} else {
unsigned int idle_count = array_count(&ppool->idle_conns);
/* kill duplicate connections quicker;
linearly based on the number of connections */
i_assert(array_count(&ppool->conns) >= idle_count + 1);
timeout = (set->max_parallel_connections - idle_count) *
(set->max_idle_time_msecs / set->max_parallel_connections);
}
e_debug(conn->event,
"No more requests queued; going idle (timeout = %u msecs)",
timeout);
conn->to_idle = timeout_add_to(conn->conn.ioloop, timeout,
http_client_connection_idle_timeout, conn);
array_append(&ppool->idle_conns, &conn, 1);
}
}
static void
http_client_connection_stop_idle(struct http_client_connection *conn)
{
struct http_client_connection *const *conn_idx;
ARRAY_TYPE(http_client_connection) *conn_arr;
if (conn->to_idle != NULL)
timeout_remove(&conn->to_idle);
conn_arr = &conn->ppool->idle_conns;
array_foreach(conn_arr, conn_idx) {
if (*conn_idx == conn) {
array_delete(conn_arr, array_foreach_idx(conn_arr, conn_idx), 1);
break;
}
}
}
void http_client_connection_claim_idle(struct http_client_connection *conn,
struct http_client_peer *peer)
{
e_debug(conn->event, "Claimed as idle");
i_assert(peer->ppool == conn->ppool);
http_client_connection_stop_idle(conn);
if (conn->peer == NULL || conn->peer != peer) {
http_client_connection_detach_peer(conn);
conn->peer = peer;
conn->debug = peer->client->set.debug;
array_append(&peer->conns, &conn, 1);
}
}
static void
http_client_connection_request_timeout(struct http_client_connection *conn)
{
conn->conn.input->stream_errno = ETIMEDOUT;
http_client_connection_abort_temp_error(&conn,
HTTP_CLIENT_REQUEST_ERROR_TIMED_OUT, "Request timed out");
}
void http_client_connection_start_request_timeout(
struct http_client_connection *conn)
{
struct http_client_peer *peer = conn->peer;
struct http_client *client = peer->client;
const struct http_client_settings *set = &client->set;
unsigned int timeout_msecs =
set->request_timeout_msecs;
if (conn->pending_request != NULL)
return;
i_assert(array_is_created(&conn->request_wait_list));
if (array_count(&conn->request_wait_list) > 0) {
struct http_client_request *const *requestp;
requestp = array_idx(&conn->request_wait_list, 0);
timeout_msecs = (*requestp)->attempt_timeout_msecs;
}
if (timeout_msecs == 0)
;
else if (conn->to_requests != NULL)
timeout_reset(conn->to_requests);
else {
conn->to_requests = timeout_add_to(
conn->conn.ioloop, timeout_msecs,
http_client_connection_request_timeout, conn);
}
}
void http_client_connection_reset_request_timeout(
struct http_client_connection *conn)
{
if (conn->to_requests != NULL)
timeout_reset(conn->to_requests);
}
void http_client_connection_stop_request_timeout(
struct http_client_connection *conn)
{
timeout_remove(&conn->to_requests);
}
static void
http_client_connection_continue_timeout(struct http_client_connection *conn)
{
struct http_client_peer_shared *pshared = conn->ppool->peer;
struct http_client_request *const *wait_reqs;
struct http_client_request *req;
unsigned int wait_count;
const char *error;
i_assert(conn->pending_request == NULL);
timeout_remove(&conn->to_response);
pshared->no_payload_sync = TRUE;
e_debug(conn->event,
"Expected 100-continue response timed out; sending payload anyway");
wait_reqs = array_get(&conn->request_wait_list, &wait_count);
i_assert(wait_count == 1);
req = wait_reqs[wait_count-1];
req->payload_sync_continue = TRUE;
if (http_client_request_send_more(req, FALSE, &error) < 0) {
http_client_connection_lost(&conn,
t_strdup_printf("Failed to send request: %s", error));
}
}
int http_client_connection_next_request(struct http_client_connection *conn)
{
struct http_client_peer *peer = conn->peer;
struct http_client_peer_shared *pshared = conn->ppool->peer;
struct http_client_request *req = NULL;
const char *error;
bool pipelined;
int ret;
if ((ret=http_client_connection_check_ready(conn)) <= 0) {
if (ret == 0) {
e_debug(conn->event, "Not ready for next request");
}
return ret;
}
/* claim request, but no urgent request can be second in line */
pipelined = array_count(&conn->request_wait_list) > 0 ||
conn->pending_request != NULL;
req = http_client_peer_claim_request(peer, pipelined);
if (req == NULL)
return 0;
i_assert(req->state == HTTP_REQUEST_STATE_QUEUED);
http_client_connection_stop_idle(conn);
req->payload_sync_continue = FALSE;
if (pshared->no_payload_sync)
req->payload_sync = FALSE;
/* add request to wait list and add a reference */
array_append(&conn->request_wait_list, &req, 1);
http_client_connection_ref_request(conn, req);
e_debug(conn->event, "Claimed request %s",
http_client_request_label(req));
if (http_client_request_send(req, pipelined, &error) < 0) {
http_client_connection_lost(&conn,
t_strdup_printf("Failed to send request: %s", error));
return -1;
}
if (req->connect_tunnel)
conn->tunneling = TRUE;
/* RFC 7231, Section 5.1.1: Expect
o A client that sends a 100-continue expectation is not required to
wait for any specific length of time; such a client MAY proceed to
send the message body even if it has not yet received a response.
Furthermore, since 100 (Continue) responses cannot be sent through
an HTTP/1.0 intermediary, such a client SHOULD NOT wait for an
indefinite period before sending the message body.
*/
if (req->payload_sync && !pshared->seen_100_response) {
i_assert(!pipelined);
i_assert(req->payload_chunked || req->payload_size > 0);
i_assert(conn->to_response == NULL);
conn->to_response = timeout_add_to(conn->conn.ioloop,
HTTP_CLIENT_CONTINUE_TIMEOUT_MSECS,
http_client_connection_continue_timeout, conn);
}
return 1;
}
static void http_client_connection_destroy(struct connection *_conn)
{
struct http_client_connection *conn =
(struct http_client_connection *)_conn;
const char *error;
unsigned int msecs;
switch (_conn->disconnect_reason) {
case CONNECTION_DISCONNECT_CONNECT_TIMEOUT:
if (conn->connected_timestamp.tv_sec == 0) {
msecs = timeval_diff_msecs(&ioloop_timeval,
&conn->connect_start_timestamp);
error = t_strdup_printf(
"connect(%s) failed: Connection timed out in %u.%03u secs",
_conn->name, msecs/1000, msecs%1000);
} else {
msecs = timeval_diff_msecs(&ioloop_timeval,
&conn->connected_timestamp);
error = t_strdup_printf(
"SSL handshaking with %s failed: Connection timed out in %u.%03u secs",
_conn->name, msecs/1000, msecs%1000);
}
e_debug(conn->event, "%s", error);
http_client_connection_failure(conn, error);
break;
case CONNECTION_DISCONNECT_CONN_CLOSED:
if (conn->connect_failed) {
i_assert(!array_is_created(&conn->request_wait_list) ||
array_count(&conn->request_wait_list) == 0);
break;
}
http_client_connection_lost(&conn,
(_conn->input == NULL ? NULL :
i_stream_get_error(_conn->input)));
return;
default:
break;
}
http_client_connection_close(&conn);
}
static void http_client_payload_finished(struct http_client_connection *conn)
{
timeout_remove(&conn->to_input);
connection_input_resume(&conn->conn);
if (array_count(&conn->request_wait_list) > 0)
http_client_connection_start_request_timeout(conn);
}
static void
http_client_payload_destroyed_timeout(struct http_client_connection *conn)
{
if (conn->close_indicated) {
http_client_connection_server_close(&conn);
return;
}
http_client_connection_input(&conn->conn);
}
static void http_client_payload_destroyed(struct http_client_request *req)
{
struct http_client_connection *conn = req->conn;
i_assert(conn != NULL);
i_assert(conn->pending_request == req);
i_assert(conn->incoming_payload != NULL);
i_assert(conn->conn.io == NULL);
e_debug(conn->event,
"Response payload stream destroyed (%u ms after initial response)",
timeval_diff_msecs(&ioloop_timeval, &req->response_time));
/* 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);
/* drop reference from connection */
if (http_client_connection_unref_request(conn, &conn->pending_request)) {
/* finish request if not already aborted */
http_client_request_finish(req);
}
conn->incoming_payload = NULL;
/* 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 a new request if
necessary. */
if (!conn->disconnected) {
conn->to_input = timeout_add_short_to(
conn->conn.ioloop, 0,
http_client_payload_destroyed_timeout, conn);
}
/* room for new requests */
if (http_client_connection_check_ready(conn) > 0)
http_client_peer_trigger_request_handler(conn->peer);
}
void http_client_connection_request_destroyed(
struct http_client_connection *conn, struct http_client_request *req)
{
struct istream *payload;
i_assert(req->conn == conn);
if (conn->pending_request != req)
return;
e_debug(conn->event, "Pending request destroyed prematurely");
payload = conn->incoming_payload;
if (payload == NULL) {
/* payload already gone */
return;
}
/* destroy the payload, so that the timeout istream is closed */
i_stream_ref(payload);
i_stream_destroy(&payload);
payload = conn->incoming_payload;
if (payload == NULL) {
/* not going to happen, but check for it anyway */
return;
}
/* the application still holds a reference to the payload stream, but it
is closed and we don't care about it anymore, so act as though it is
destroyed. */
i_stream_remove_destroy_callback(payload,
http_client_payload_destroyed);
http_client_payload_destroyed(req);
}
static bool
http_client_connection_return_response(
struct http_client_connection *conn,
struct http_client_request *req,
struct http_response *response)
{
struct http_client_peer_shared *pshared = conn->ppool->peer;
struct istream *payload;
bool retrying;
i_assert(!conn->in_req_callback);
i_assert(conn->incoming_payload == NULL);
i_assert(conn->pending_request == NULL);
http_client_connection_ref(conn);
http_client_connection_ref_request(conn, req);
req->state = HTTP_REQUEST_STATE_GOT_RESPONSE;
if (response->payload != NULL) {
/* wrap the stream to capture the destroy event without
destroying the actual payload stream. we are already expected
to be on the correct ioloop, so there should be no need to
switch the stream's ioloop here. */
conn->incoming_payload = response->payload =
i_stream_create_timeout(response->payload,
req->attempt_timeout_msecs);
i_stream_add_destroy_callback(response->payload,
http_client_payload_destroyed,
req);
/* the callback may add its own I/O, so we need to remove
our one before calling it */
connection_input_halt(&conn->conn);
/* we've received the request itself, and we can't reset the
timeout during the payload reading. */
http_client_connection_stop_request_timeout(conn);
}
conn->in_req_callback = TRUE;
retrying = !http_client_request_callback(req, response);
if (conn->disconnected) {
/* the callback managed to get this connection disconnected */
if (!retrying)
http_client_request_finish(req);
http_client_connection_unref_request(conn, &req);
http_client_connection_unref(&conn);
return FALSE;
}
conn->in_req_callback = FALSE;
if (retrying) {
/* retrying, don't destroy the request */
if (response->payload != NULL) {
i_stream_remove_destroy_callback(conn->incoming_payload,
http_client_payload_destroyed);
i_stream_unref(&conn->incoming_payload);
connection_input_resume(&conn->conn);
}
http_client_connection_unref_request(conn, &req);
return http_client_connection_unref(&conn);
}
if (response->payload != NULL) {
req->state = HTTP_REQUEST_STATE_PAYLOAD_IN;
payload = response->payload;
response->payload = NULL;
/* maintain request reference while payload is pending */
conn->pending_request = req;
/* request is dereferenced in payload destroy callback */
i_stream_unref(&payload);
if (conn->to_input != NULL && conn->conn.input != NULL) {
/* already finished reading the payload */
http_client_payload_finished(conn);
}
} else {
http_client_request_finish(req);
http_client_connection_unref_request(conn, &req);
}
if (conn->incoming_payload == NULL && conn->conn.input != NULL) {
i_assert(conn->conn.io != NULL ||
pshared->addr.type == HTTP_CLIENT_PEER_ADDR_RAW);
return http_client_connection_unref(&conn);
}
http_client_connection_unref(&conn);
return FALSE;
}
static void http_client_connection_input(struct connection *_conn)
{
struct http_client_connection *conn =
(struct http_client_connection *)_conn;
struct http_client_peer *peer = conn->peer;
struct http_client_peer_shared *pshared = conn->ppool->peer;
struct http_response response;
struct http_client_request *const *reqs;
struct http_client_request *req = NULL, *req_ref;
enum http_response_payload_type payload_type;
unsigned int count;
int finished = 0, ret;
const char *error;
i_assert(conn->incoming_payload == NULL);
_conn->last_input = ioloop_time;
if (conn->ssl_iostream != NULL &&
!ssl_iostream_is_handshaked(conn->ssl_iostream)) {
/* finish SSL negotiation by reading from input stream */
while ((ret=i_stream_read(conn->conn.input)) > 0 || ret == -2) {
if (ssl_iostream_is_handshaked(conn->ssl_iostream))
break;
}
if (ret < 0) {
int stream_errno = conn->conn.input->stream_errno;
/* failed somehow */
i_assert(ret != -2);
error = t_strdup_printf(
"SSL handshaking with %s failed: "
"read(%s) failed: %s",
_conn->name,
i_stream_get_name(conn->conn.input),
stream_errno != 0 ?
i_stream_get_error(conn->conn.input) : "EOF");
http_client_connection_failure(conn, error);
e_debug(conn->event, "%s", error);
http_client_connection_close(&conn);
return;
}
if (!ssl_iostream_is_handshaked(conn->ssl_iostream)) {
/* not finished */
i_assert(ret == 0);
return;
}
/* ready for first request */
http_client_connection_ready(conn);
}
if (conn->to_input != NULL) {
/* We came here from a timeout added by
http_client_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_client_payload_finished(conn);
finished++;
}
/* we've seen activity from the server; reset request timeout */
http_client_connection_reset_request_timeout(conn);
/* get first waiting request */
reqs = array_get(&conn->request_wait_list, &count);
if (count > 0) {
req = reqs[0];
/* determine whether to expect a response payload */
payload_type = http_client_request_get_payload_type(req);
} else {
req = NULL;
payload_type = HTTP_RESPONSE_PAYLOAD_TYPE_ALLOWED;
}
/* drop connection with broken output if last possible input was
received */
if (conn->output_broken && (count == 0 ||
(count == 1 && req->state == HTTP_REQUEST_STATE_ABORTED))) {
http_client_connection_server_close(&conn);
return;
}
while ((ret=http_response_parse_next
(conn->http_parser, payload_type, &response, &error)) > 0) {
bool aborted, early = FALSE;
if (req == NULL) {
/* server sent response without any requests in the wait list */
if (response.status == 408) {
e_debug(conn->event,
"Server explicitly closed connection: 408 %s",
response.reason);
} else {
e_debug(conn->event,
"Got unexpected input from server: %u %s",
response.status, response.reason);
}
http_client_connection_close(&conn);
return;
}
req->response_time = ioloop_timeval;
/* Got some response; cancel response timeout */
timeout_remove(&conn->to_response);
/* RFC 7231, Section 6.2:
A client MUST be able to parse one or more 1xx responses received
prior to a final response, even if the client does not expect one. A
user agent MAY ignore unexpected 1xx responses.
*/
if (req->payload_sync && response.status == 100) {
if (req->payload_sync_continue) {
e_debug(conn->event,
"Got 100-continue response after timeout");
continue;
}