-
Notifications
You must be signed in to change notification settings - Fork 0
/
outgoing.c
1963 lines (1655 loc) · 65.2 KB
/
outgoing.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 2002-2004 Justin Erenkrantz and Greg Stein
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <apr_pools.h>
#include <apr_poll.h>
#include <apr_version.h>
#include <apr_portable.h>
#include <apr_strings.h>
#include "serf.h"
#include "serf_bucket_util.h"
#include "serf_private.h"
/* cleanup for sockets */
static apr_status_t clean_skt(void *data)
{
serf_connection_t *conn = data;
apr_status_t status = APR_SUCCESS;
if (conn->skt) {
status = apr_socket_close(conn->skt);
conn->skt = NULL;
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"closed socket, status %d\n", status);
serf_config_remove_value(conn->config, SERF_CONFIG_CONN_LOCALIP);
serf_config_remove_value(conn->config, SERF_CONFIG_CONN_REMOTEIP);
}
return status;
}
static apr_status_t clean_resp(void *data)
{
serf_request_t *request = data;
/* The request's RESPOOL is being cleared. */
/* If the response has allocated some buckets, then destroy them (since
the bucket may hold resources other than memory in RESPOOL). Also
make sure to set their fields to NULL so connection closure does
not attempt to free them again. */
if (request->resp_bkt) {
serf_bucket_destroy(request->resp_bkt);
request->resp_bkt = NULL;
}
if (request->req_bkt) {
serf_bucket_destroy(request->req_bkt);
request->req_bkt = NULL;
}
/* ### should we worry about debug stuff, like that performed in
### destroy_request()? should we worry about calling req->handler
### to notify this "cancellation" due to pool clearing? */
/* This pool just got cleared/destroyed. Don't try to destroy the pool
(again) when the request is canceled. */
request->respool = NULL;
return APR_SUCCESS;
}
/* cleanup for conns */
static apr_status_t clean_conn(void *data)
{
serf_connection_t *conn = data;
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"cleaning up connection 0x%x\n", conn);
serf_connection_close(conn);
return APR_SUCCESS;
}
/* Check if there is data waiting to be sent over the socket. This can happen
in two situations:
- The connection queue has atleast one request with unwritten data.
- All requests are written and the ssl layer wrote some data while reading
the response. This can happen when the server triggers a renegotiation,
e.g. after the first and only request on that connection was received.
Returns 1 if data is pending on CONN, NULL if not.
If NEXT_REQ is not NULL, it will be filled in with the next available request
with unwritten data. */
static int
request_or_data_pending(serf_request_t **next_req, serf_connection_t *conn)
{
/* Skip all requests that have been written completely but we're still
waiting for a response. */
serf_request_t *request = conn->unwritten_reqs;
if (next_req)
*next_req = request;
if (request != NULL) {
return 1;
} else if (conn->ostream_head) {
const char *dummy;
apr_size_t len;
apr_status_t status;
status = serf_bucket_peek(conn->ostream_head, &dummy,
&len);
if (!SERF_BUCKET_READ_ERROR(status) && len) {
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"All requests written but still data pending.\n");
return 1;
}
}
return 0;
}
/* Update the pollset for this connection. We tweak the pollset based on
* whether we want to read and/or write, given conditions within the
* connection. If the connection is not (yet) in the pollset, then it
* will be added.
*/
apr_status_t serf__conn_update_pollset(serf_connection_t *conn)
{
serf_context_t *ctx = conn->ctx;
apr_status_t status;
apr_pollfd_t desc = { 0 };
if (!conn->skt) {
return APR_SUCCESS;
}
/* Remove the socket from the poll set. */
desc.desc_type = APR_POLL_SOCKET;
desc.desc.s = conn->skt;
desc.reqevents = conn->reqevents;
status = ctx->pollset_rm(ctx->pollset_baton,
&desc, &conn->baton);
if (status && !APR_STATUS_IS_NOTFOUND(status))
return status;
/* Now put it back in with the correct read/write values. */
desc.reqevents = APR_POLLHUP | APR_POLLERR;
if ((conn->written_reqs || conn->unwritten_reqs) &&
conn->state != SERF_CONN_INIT) {
/* If there are any outstanding events, then we want to read. */
/* ### not true. we only want to read IF we have sent some data */
desc.reqevents |= APR_POLLIN;
/* Don't write if OpenSSL told us that it needs to read data first. */
if (conn->stop_writing != 1) {
/* If the connection is not closing down and
* has unwritten data or
* there are any requests that still have buckets to write out,
* then we want to write.
*/
if (conn->vec_len &&
conn->state != SERF_CONN_CLOSING)
desc.reqevents |= APR_POLLOUT;
else {
if ((conn->probable_keepalive_limit &&
conn->completed_requests > conn->probable_keepalive_limit) ||
(conn->max_outstanding_requests &&
conn->completed_requests - conn->completed_responses >=
conn->max_outstanding_requests)) {
/* we wouldn't try to write any way right now. */
}
else if (request_or_data_pending(NULL, conn)) {
desc.reqevents |= APR_POLLOUT;
}
}
}
}
/* If we can have async responses, always look for something to read. */
if (conn->async_responses) {
desc.reqevents |= APR_POLLIN;
}
/* save our reqevents, so we can pass it in to remove later. */
conn->reqevents = desc.reqevents;
/* Note: even if we don't want to read/write this socket, we still
* want to poll it for hangups and errors.
*/
return ctx->pollset_add(ctx->pollset_baton,
&desc, &conn->baton);
}
#ifdef SERF_DEBUG_BUCKET_USE
/* Make sure all response buckets were drained. */
static void check_buckets_drained(serf_connection_t *conn)
{
serf_request_t *request = conn->requests;
for ( ; request ; request = request->next ) {
if (request->resp_bkt != NULL) {
/* ### crap. can't do this. this allocator may have un-drained
* ### REQUEST buckets.
*/
/* serf_debug__entered_loop(request->resp_bkt->allocator); */
/* ### for now, pretend we closed the conn (resets the tracking) */
serf_debug__closed_conn(request->resp_bkt->allocator);
}
}
}
#endif
static void destroy_ostream(serf_connection_t *conn)
{
if (conn->ostream_head != NULL) {
serf_bucket_destroy(conn->ostream_head);
conn->ostream_head = NULL;
conn->ostream_tail = NULL;
}
}
static apr_status_t detect_eof(void *baton, serf_bucket_t *aggregate_bucket)
{
serf_connection_t *conn = baton;
conn->hit_eof = 1;
return APR_EAGAIN;
}
static apr_status_t do_conn_setup(serf_connection_t *conn)
{
apr_status_t status;
serf_bucket_t *ostream;
/* ### dunno what the hell this is about. this latency stuff got
### added, and who knows whether it should stay... */
conn->latency = apr_time_now() - conn->connect_time;
if (conn->ostream_head == NULL) {
conn->ostream_head = serf_bucket_aggregate_create(conn->allocator);
}
if (conn->ostream_tail == NULL) {
conn->ostream_tail = serf__bucket_stream_create(conn->allocator,
detect_eof,
conn);
}
ostream = conn->ostream_tail;
status = (*conn->setup)(conn->skt,
&conn->stream,
&ostream,
conn->setup_baton,
conn->pool);
if (status) {
/* extra destroy here since it wasn't added to the head bucket yet. */
serf_bucket_destroy(conn->ostream_tail);
destroy_ostream(conn);
return status;
}
/* Share the configuration with all the buckets in the newly created output
chain (see PLAIN or ENCRYPTED scenario's), including the request buckets
created by the application (ostream_tail will handle this for us). */
serf_bucket_set_config(conn->ostream_head, conn->config);
/* Share the configuration with the ssl_decrypt and socket buckets. The
response buckets wrapping the ssl_decrypt/socket buckets won't get the
config automatically because they are upstream. */
serf_bucket_set_config(conn->stream, conn->config);
serf_bucket_aggregate_append(conn->ostream_head,
ostream);
/* We typically have one of two scenarios, based on whether the
application decided to encrypt this connection:
PLAIN:
conn->stream = SOCKET(skt)
conn->ostream_head = AGGREGATE(ostream_tail)
conn->ostream_tail = STREAM(<detect_eof>, REQ1, REQ2, ...)
ENCRYPTED:
conn->stream = DECRYPT(SOCKET(skt))
conn->ostream_head = AGGREGATE(ENCRYPT(ostream_tail))
conn->ostream_tail = STREAM(<detect_eof>, REQ1, REQ2, ...)
where STREAM is an internal variant of AGGREGATE.
*/
return status;
}
/* Set up the input and output stream buckets.
When a tunnel over an http proxy is needed, create a socket bucket and
empty aggregate bucket for sending and receiving unencrypted requests
over the socket.
After the tunnel is there, or no tunnel was needed, ask the application
to create the input and output buckets, which should take care of the
[en/de]cryption.
*/
static apr_status_t prepare_conn_streams(serf_connection_t *conn,
serf_bucket_t **ostreamt,
serf_bucket_t **ostreamh)
{
apr_status_t status;
/* Do we need a SSL tunnel first? */
if (conn->state == SERF_CONN_CONNECTED) {
/* If the connection does not have an associated bucket, then
* call the setup callback to get one.
*/
if (conn->stream == NULL) {
status = do_conn_setup(conn);
if (status) {
return status;
}
}
*ostreamt = conn->ostream_tail;
*ostreamh = conn->ostream_head;
} else {
/* state == SERF_CONN_SETUP_SSLTUNNEL */
/* SSL tunnel needed and not set up yet, get a direct unencrypted
stream for this socket */
if (conn->stream == NULL) {
conn->stream = serf_context_bucket_socket_create(conn->ctx,
conn->skt,
conn->allocator);
}
/* Don't create the ostream bucket chain including the ssl_encrypt
bucket yet. This ensure the CONNECT request is sent unencrypted
to the proxy. */
*ostreamt = *ostreamh = conn->ssltunnel_ostream;
}
return APR_SUCCESS;
}
static void store_ipaddresses_in_config(serf_config_t *config,
apr_socket_t *skt)
{
apr_sockaddr_t *sa;
if (apr_socket_addr_get(&sa, APR_LOCAL, skt) == APR_SUCCESS) {
char buf[32];
apr_sockaddr_ip_getbuf(buf, 32, sa);
serf_config_set_stringf(config, SERF_CONFIG_CONN_LOCALIP,
"%s:%d", buf, sa->port);
}
if (apr_socket_addr_get(&sa, APR_REMOTE, skt) == APR_SUCCESS) {
char buf[32];
apr_sockaddr_ip_getbuf(buf, 32, sa);
serf_config_set_stringf(config, SERF_CONFIG_CONN_REMOTEIP,
"%s:%d", buf, sa->port);
}
}
/* Create and connect sockets for any connections which don't have them
* yet. This is the core of our lazy-connect behavior.
*/
apr_status_t serf__open_connections(serf_context_t *ctx)
{
int i;
for (i = ctx->conns->nelts; i--; ) {
serf_connection_t *conn = GET_CONN(ctx, i);
apr_status_t status;
apr_socket_t *skt;
conn->seen_in_pollset = 0;
if (conn->skt != NULL) {
#ifdef SERF_DEBUG_BUCKET_USE
check_buckets_drained(conn);
#endif
continue;
}
/* Delay opening until we have something to deliver! */
if (conn->unwritten_reqs == NULL) {
continue;
}
apr_pool_clear(conn->skt_pool);
apr_pool_cleanup_register(conn->skt_pool, conn, clean_skt,
apr_pool_cleanup_null);
status = apr_socket_create(&skt, conn->address->family,
SOCK_STREAM,
#if APR_MAJOR_VERSION > 0
APR_PROTO_TCP,
#endif
conn->skt_pool);
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"created socket for conn 0x%x, status %d\n", conn, status);
if (status != APR_SUCCESS)
return status;
/* Set the socket to be non-blocking */
if ((status = apr_socket_timeout_set(skt, 0)) != APR_SUCCESS)
return status;
/* Disable Nagle's algorithm */
if ((status = apr_socket_opt_set(skt,
APR_TCP_NODELAY, 1)) != APR_SUCCESS)
return status;
/* Configured. Store it into the connection now. */
conn->skt = skt;
/* Remember time when we started connecting to server to calculate
network latency. */
conn->connect_time = apr_time_now();
/* Now that the socket is set up, let's connect it. This should
* return immediately.
*/
status = apr_socket_connect(skt, conn->address);
store_ipaddresses_in_config(conn->config, skt);
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"connected socket for conn 0x%x, status %d\n", conn, status);
if (status != APR_SUCCESS) {
if (!APR_STATUS_IS_EINPROGRESS(status))
return status;
}
status = serf_config_set_string(conn->config,
SERF_CONFIG_CONN_PIPELINING,
(conn->max_outstanding_requests != 1 &&
conn->pipelining == 1) ? "Y" : "N");
if (status)
return status;
/* Flag our pollset as dirty now that we have a new socket. */
conn->dirty_conn = 1;
ctx->dirty_pollset = 1;
/* If the authentication was already started on another connection,
prepare this connection (it might be possible to skip some
part of the handshaking). */
if (ctx->proxy_address) {
status = serf__auth_setup_connection(PROXY, conn);
if (status){
return status;
}
}
status = serf__auth_setup_connection(HOST, conn);
if (status)
return status;
/* Does this connection require a SSL tunnel over the proxy? */
if (ctx->proxy_address && strcmp(conn->host_info.scheme, "https") == 0)
serf__ssltunnel_connect(conn);
else {
conn->state = SERF_CONN_CONNECTED;
status = do_conn_setup(conn);
if (status)
return status;
}
}
return APR_SUCCESS;
}
static apr_status_t no_more_writes(serf_connection_t *conn)
{
/* Note that we should hold new requests until we open our new socket. */
conn->state = SERF_CONN_CLOSING;
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"stop writing on conn 0x%x\n", conn);
/* Clear our iovec. */
conn->vec_len = 0;
/* Update the pollset to know we don't want to write on this socket any
* more.
*/
conn->dirty_conn = 1;
conn->ctx->dirty_pollset = 1;
return APR_SUCCESS;
}
/* Read the 'Connection' header from the response. Return SERF_ERROR_CLOSING if
* the header contains value 'close' indicating the server is closing the
* connection right after this response.
* Otherwise returns APR_SUCCESS.
*/
static apr_status_t is_conn_closing(serf_bucket_t *response)
{
serf_bucket_t *hdrs;
const char *val;
hdrs = serf_bucket_response_get_headers(response);
val = serf_bucket_headers_get(hdrs, "Connection");
if (val && strcasecmp("close", val) == 0)
{
return SERF_ERROR_CLOSING;
}
return APR_SUCCESS;
}
static void link_requests(serf_request_t **list, serf_request_t **tail,
serf_request_t *request)
{
if (*list == NULL) {
*list = request;
*tail = request;
}
else {
(*tail)->next = request;
*tail = request;
}
}
static apr_status_t destroy_request(serf_request_t *request)
{
serf_connection_t *conn = request->conn;
/* The request and response buckets are no longer needed,
nor is the request's pool. */
if (request->resp_bkt) {
serf_debug__closed_conn(request->resp_bkt->allocator);
serf_bucket_destroy(request->resp_bkt);
request->resp_bkt = NULL;
}
if (request->req_bkt) {
serf_debug__closed_conn(request->req_bkt->allocator);
serf_bucket_destroy(request->req_bkt);
request->req_bkt = NULL;
}
if (request->respool) {
serf_debug__bucket_alloc_check(request->allocator);
/* ### unregister the pool cleanup for self? */
apr_pool_destroy(request->respool);
}
serf_bucket_mem_free(conn->allocator, request);
return APR_SUCCESS;
}
static apr_status_t cancel_request(serf_request_t *request,
serf_request_t **list,
int notify_request)
{
/* If we haven't run setup, then we won't have a handler to call. */
if (request->handler && notify_request) {
/* We actually don't care what the handler returns.
* We have bigger matters at hand.
*/
(*request->handler)(request, NULL, request->handler_baton,
request->respool);
}
if (*list == request) {
*list = request->next;
}
else {
serf_request_t *scan = *list;
while (scan->next && scan->next != request)
scan = scan->next;
if (scan->next) {
scan->next = scan->next->next;
}
}
return destroy_request(request);
}
/* Calculate the length of a linked list of requests. */
static unsigned int req_list_length(serf_request_t *req)
{
unsigned int length = 0;
while (req) {
length++;
req = req->next;
}
return length;
}
static apr_status_t remove_connection(serf_context_t *ctx,
serf_connection_t *conn)
{
apr_pollfd_t desc = { 0 };
desc.desc_type = APR_POLL_SOCKET;
desc.desc.s = conn->skt;
desc.reqevents = conn->reqevents;
return ctx->pollset_rm(ctx->pollset_baton,
&desc, &conn->baton);
}
/* A socket was closed, inform the application. */
static void handle_conn_closed(serf_connection_t *conn, apr_status_t status)
{
(*conn->closed)(conn, conn->closed_baton, status,
conn->pool);
}
static apr_status_t reset_connection(serf_connection_t *conn,
int requeue_requests)
{
serf_context_t *ctx = conn->ctx;
apr_status_t status;
serf_request_t *old_reqs;
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"reset connection 0x%x\n", conn);
conn->probable_keepalive_limit = conn->completed_responses;
conn->completed_requests = 0;
conn->completed_responses = 0;
/* Clear the unwritten_reqs queue, so the application can requeue cancelled
requests on it for the new socket. */
old_reqs = conn->unwritten_reqs;
conn->unwritten_reqs = NULL;
conn->unwritten_reqs_tail = NULL;
/* First, cancel all written requests for which we haven't received a
response yet. Inform the application that the request is cancelled,
so it can requeue them if needed. */
while (conn->written_reqs) {
cancel_request(conn->written_reqs, &conn->written_reqs,
requeue_requests);
}
conn->written_reqs_tail = NULL;
/* Handle all outstanding unwritten requests.
TODO: what about a partially written request? */
while (old_reqs) {
/* If we haven't started to write the connection, bring it over
* unchanged to our new socket.
* Do not copy a CONNECT request to the new connection, the ssl tunnel
* setup code will create a new CONNECT request already.
*/
if (requeue_requests && !old_reqs->writing_started &&
!old_reqs->ssltunnel) {
serf_request_t *req = old_reqs;
old_reqs = old_reqs->next;
req->next = NULL;
link_requests(&conn->unwritten_reqs, &conn->unwritten_reqs_tail,
req);
}
else {
/* We don't want to requeue the request or this request was partially
written. Inform the application that the request is cancelled. */
cancel_request(old_reqs, &old_reqs, requeue_requests);
}
}
/* Requests queue has been prepared for a new socket, close the old one. */
if (conn->skt != NULL) {
remove_connection(ctx, conn);
status = clean_skt(conn);
if (conn->closed != NULL) {
handle_conn_closed(conn, status);
}
}
if (conn->stream != NULL) {
serf_bucket_destroy(conn->stream);
conn->stream = NULL;
}
destroy_ostream(conn);
/* Don't try to resume any writes */
conn->vec_len = 0;
conn->dirty_conn = 1;
conn->ctx->dirty_pollset = 1;
conn->state = SERF_CONN_INIT;
conn->status = APR_SUCCESS;
/* Let our context know that we've 'reset' the socket already. */
conn->seen_in_pollset |= APR_POLLHUP;
/* Recalculate the current list length */
conn->nr_of_written_reqs = 0;
conn->nr_of_unwritten_reqs = req_list_length(conn->unwritten_reqs);
/* Found the connection. Closed it. All done. */
return APR_SUCCESS;
}
static apr_status_t socket_writev(serf_connection_t *conn)
{
apr_size_t written;
apr_status_t status;
status = apr_socket_sendv(conn->skt, conn->vec,
conn->vec_len, &written);
if (status && !APR_STATUS_IS_EAGAIN(status))
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"socket_sendv error %d\n", status);
/* did we write everything? */
if (written) {
apr_size_t len = 0;
int i;
serf__log(LOGLVL_DEBUG, LOGCOMP_CONN, __FILE__, conn->config,
"--- socket_sendv: %d bytes. --\n", written);
for (i = 0; i < conn->vec_len; i++) {
len += conn->vec[i].iov_len;
if (written < len) {
serf__log_nopref(LOGLVL_DEBUG, LOGCOMP_RAWMSG, conn->config,
"%.*s", conn->vec[i].iov_len - (len - written),
conn->vec[i].iov_base);
if (i) {
memmove(conn->vec, &conn->vec[i],
sizeof(struct iovec) * (conn->vec_len - i));
conn->vec_len -= i;
}
conn->vec[0].iov_base = (char *)conn->vec[0].iov_base + (conn->vec[0].iov_len - (len - written));
conn->vec[0].iov_len = len - written;
break;
} else {
serf__log_nopref(LOGLVL_DEBUG, LOGCOMP_RAWMSG, conn->config,
"%.*s",
conn->vec[i].iov_len, conn->vec[i].iov_base);
}
}
if (len == written) {
conn->vec_len = 0;
}
serf__log_nopref(LOGLVL_DEBUG, LOGCOMP_RAWMSG, conn->config, "\n");
/* Log progress information */
serf__context_progress_delta(conn->ctx, 0, written);
}
return status;
}
static apr_status_t setup_request(serf_request_t *request)
{
serf_connection_t *conn = request->conn;
apr_status_t status;
/* Now that we are about to serve the request, allocate a pool. */
apr_pool_create(&request->respool, conn->pool);
request->allocator = serf_bucket_allocator_create(request->respool,
NULL, NULL);
apr_pool_cleanup_register(request->respool, request,
clean_resp, apr_pool_cleanup_null);
/* Fill in the rest of the values for the request. */
status = request->setup(request, request->setup_baton,
&request->req_bkt,
&request->acceptor,
&request->acceptor_baton,
&request->handler,
&request->handler_baton,
request->respool);
return status;
}
/* write data out to the connection */
static apr_status_t write_to_connection(serf_connection_t *conn)
{
if (conn->probable_keepalive_limit &&
conn->completed_requests > conn->probable_keepalive_limit) {
conn->dirty_conn = 1;
conn->ctx->dirty_pollset = 1;
/* backoff for now. */
return APR_SUCCESS;
}
/* Keep reading and sending until we run out of stuff to read, or
* writing would block.
*/
while (1) {
serf_request_t *request;
int stop_reading = 0;
apr_status_t status;
apr_status_t read_status;
serf_bucket_t *ostreamt;
serf_bucket_t *ostreamh;
int reqs_in_progress;
reqs_in_progress = conn->completed_requests - conn->completed_responses;
/* If we're setting up an ssl tunnel, we can't send real requests
at yet, as they need to be encrypted and our encrypt buckets
aren't created yet as we still need to read the unencrypted
response of the CONNECT request. */
if (conn->state == SERF_CONN_SETUP_SSLTUNNEL && reqs_in_progress > 0)
{
return APR_SUCCESS;
}
/* We try to limit the number of in-flight requests so that we
don't have to repeat too many if the connection drops. */
if (conn->max_outstanding_requests
&& (reqs_in_progress >= conn->max_outstanding_requests))
{
/* backoff for now. */
return APR_SUCCESS;
}
/* If pipelining is disabled, max. one request should be in progress */
if (!conn->pipelining && reqs_in_progress > 0)
{
/* backoff for now. */
return APR_SUCCESS;
}
/* If we have unwritten data, then write what we can. */
while (conn->vec_len) {
status = socket_writev(conn);
/* If the write would have blocked, then we're done. Don't try
* to write anything else to the socket.
*/
if (APR_STATUS_IS_EAGAIN(status))
return APR_SUCCESS;
if (APR_STATUS_IS_EPIPE(status)
|| APR_STATUS_IS_ECONNRESET(status)
|| APR_STATUS_IS_ECONNABORTED(status))
return no_more_writes(conn);
if (status)
return status;
}
/* ### can we have a short write, yet no EAGAIN? a short write
### would imply unwritten_len > 0 ... */
/* assert: unwritten_len == 0. */
/* We may need to move forward to a request which has something
* to write.
*/
if (!request_or_data_pending(&request, conn)) {
/* No more requests (with data) are registered with the
* connection, and no data is pending on the outgoing stream.
* Let's update the pollset so that we don't try to write to this
* socket again.
*/
conn->dirty_conn = 1;
conn->ctx->dirty_pollset = 1;
return APR_SUCCESS;
}
status = prepare_conn_streams(conn, &ostreamt, &ostreamh);
if (status) {
return status;
}
if (request) {
if (request->req_bkt == NULL) {
read_status = setup_request(request);
if (read_status) {
/* Something bad happened. Propagate any errors. */
return read_status;
}
}
if (!request->writing_started) {
request->writing_started = 1;
serf_bucket_aggregate_append(ostreamt, request->req_bkt);
}
}
/* ### optimize at some point by using read_for_sendfile */
/* TODO: now that read_iovec will effectively try to return as much
data as available, we probably don't want to read ALL_AVAIL, but
a lower number, like the size of one or a few TCP packets, the
available TCP buffer size ... */
read_status = serf_bucket_read_iovec(ostreamh,
SERF_READ_ALL_AVAIL,
IOV_MAX,
conn->vec,
&conn->vec_len);
if (!conn->hit_eof) {
if (APR_STATUS_IS_EAGAIN(read_status)) {
/* We read some stuff, but should not try to read again. */
stop_reading = 1;
}
else if (read_status == SERF_ERROR_WAIT_CONN) {
/* The bucket told us that it can't provide more data until
more data is read from the socket. This normally happens
during a SSL handshake.
We should avoid looking for writability for a while so
that (hopefully) something will appear in the bucket so
we can actually write something. otherwise, we could
end up in a CPU spin: socket wants something, but we
don't have anything (and keep returning EAGAIN)
*/
conn->stop_writing = 1;
conn->dirty_conn = 1;
conn->ctx->dirty_pollset = 1;
}
else if (read_status && !APR_STATUS_IS_EOF(read_status)) {
/* Something bad happened. Propagate any errors. */
return read_status;
}
}
/* If we got some data, then deliver it. */
/* ### what to do if we got no data?? is that a problem? */
if (conn->vec_len > 0) {
status = socket_writev(conn);
/* If we can't write any more, or an error occurred, then
* we're done here.
*/
if (APR_STATUS_IS_EAGAIN(status))
return APR_SUCCESS;
if (APR_STATUS_IS_EPIPE(status)
|| APR_STATUS_IS_ECONNRESET(status)
|| APR_STATUS_IS_ECONNABORTED(status))
return no_more_writes(conn);
if (status)
return status;
}
if (read_status == SERF_ERROR_WAIT_CONN) {
stop_reading = 1;
conn->stop_writing = 1;
conn->dirty_conn = 1;
conn->ctx->dirty_pollset = 1;
}
else if (request && read_status && conn->hit_eof &&
conn->vec_len == 0) {
/* If we hit the end of the request bucket and all of its data has
* been written, then clear it out to signify that we're done
* sending the request. On the next iteration through this loop:
* - if there are remaining bytes they will be written, and as the
* request bucket will be completely read it will be destroyed then.
* - we'll see if there are other requests that need to be sent
* ("pipelining").
*/
conn->hit_eof = 0;
serf_bucket_destroy(request->req_bkt);
request->req_bkt = NULL;
/* Move the request to the written queue */
link_requests(&conn->written_reqs, &conn->written_reqs_tail,
request);
conn->nr_of_written_reqs++;
conn->unwritten_reqs = conn->unwritten_reqs->next;
conn->nr_of_unwritten_reqs--;
request->next = NULL;
/* If our connection has async responses enabled, we're not
* going to get a reply back, so kill the request.
*/
if (conn->async_responses) {
conn->unwritten_reqs = request->next;
conn->nr_of_unwritten_reqs--;
destroy_request(request);
}
conn->completed_requests++;
if (conn->probable_keepalive_limit &&
conn->completed_requests > conn->probable_keepalive_limit) {
/* backoff for now. */
stop_reading = 1;
}
}
if (stop_reading) {
return APR_SUCCESS;
}
}
/* NOTREACHED */
}