11
22#include <lighttpd/base.h>
3- #include <lighttpd/throttle.h>
43#include <lighttpd/plugin_core.h>
54
65#define LI_CONNECTION_DEFAULT_CHUNKQUEUE_LIMIT (256*1024)
76
8- void li_connection_simple_tcp (liConnection * * pcon , liIOStream * stream , liConnectionSimpleTcpState * state , liIOStreamEvent event ) {
9- liConnection * con ;
10- goffset transfer_in = 0 , transfer_out = 0 ;
11-
12- transfer_in = (NULL != stream -> stream_in .out ) ? stream -> stream_in .out -> bytes_in : 0 ;
13- transfer_out = (NULL != stream -> stream_out .out ) ? stream -> stream_out .out -> bytes_out : 0 ;
14-
15- li_stream_simple_socket_io_cb_with_buffer (stream , event , & state -> read_buffer );
16-
17- /* li_stream_simple_socket_io_cb_with_buffer might lead to *pcon == NULL */
18- con = * pcon ;
19- if (NULL != con ) {
20- if (NULL != stream -> stream_in .out ) {
21- transfer_in = stream -> stream_in .out -> bytes_in - transfer_in ;
22- if (transfer_in > 0 ) {
23- li_connection_update_io_timeout (con );
24- li_vrequest_update_stats_in (con -> mainvr , transfer_in );
25- }
26- }
27- if (NULL != stream -> stream_out .out ) {
28- transfer_out = stream -> stream_out .out -> bytes_out - transfer_out ;
29- if (transfer_out > 0 ) {
30- li_connection_update_io_timeout (con );
31- li_vrequest_update_stats_out (con -> mainvr , transfer_out );
32- }
33- }
34- }
35-
36- switch (event ) {
37- case LI_IOSTREAM_DESTROY :
38- li_stream_simple_socket_close (stream , FALSE);
39- return ;
40- case LI_IOSTREAM_DISCONNECTED_DEST :
41- if (NULL != stream -> stream_in .out && !stream -> stream_in .out -> is_closed ) {
42- li_stream_simple_socket_close (stream , TRUE);
43- return ;
44- }
45- break ;
46- case LI_IOSTREAM_DISCONNECTED_SOURCE :
47- if (NULL != stream -> stream_out .out && !stream -> stream_out .out -> is_closed ) {
48- li_stream_simple_socket_close (stream , TRUE);
49- return ;
50- }
51- break ;
52- default :
53- break ;
54- }
55-
56- if ((NULL == stream -> stream_in .out || stream -> stream_in .out -> is_closed ) &&
57- !(NULL == stream -> stream_out .out || stream -> stream_out .out -> is_closed )) {
58- stream -> stream_out .out -> is_closed = TRUE;
59- li_stream_again_later (& stream -> stream_out );
60- }
61- }
62-
63-
64- typedef struct simple_tcp_connection simple_tcp_connection ;
65- struct simple_tcp_connection {
66- liIOStream * sock_stream ;
67- liConnectionSimpleTcpState simple_tcp_state ;
68- liConnection * con ;
69- };
70-
71- static void simple_tcp_io_cb (liIOStream * stream , liIOStreamEvent event ) {
72- simple_tcp_connection * data = stream -> data ;
73- LI_FORCE_ASSERT (NULL != data );
74- LI_FORCE_ASSERT (NULL == data -> con || data == data -> con -> con_sock .data );
75- LI_FORCE_ASSERT (NULL == data -> sock_stream || stream == data -> sock_stream );
76-
77- li_connection_simple_tcp (& data -> con , stream , & data -> simple_tcp_state , event );
78-
79- if (NULL != data -> con && data -> con -> out_has_all_data
80- && (NULL == stream -> stream_out .out || 0 == stream -> stream_out .out -> length )) {
81- li_stream_simple_socket_flush (stream );
82- li_connection_request_done (data -> con );
83- }
84-
85- switch (event ) {
86- case LI_IOSTREAM_DESTROY :
87- LI_FORCE_ASSERT (NULL == data -> con );
88- LI_FORCE_ASSERT (NULL == data -> sock_stream );
89- stream -> data = NULL ;
90- g_slice_free (simple_tcp_connection , data );
91- break ;
92- default :
93- break ;
94- }
95- }
96-
97- static void simple_tcp_finished (liConnection * con , gboolean aborted ) {
98- simple_tcp_connection * data = con -> con_sock .data ;
99- liIOStream * stream ;
100- if (NULL == data ) return ;
101-
102- data -> con = NULL ;
103- con -> con_sock .data = NULL ;
104- con -> con_sock .callbacks = NULL ;
105-
106- stream = data -> sock_stream ;
107- data -> sock_stream = NULL ;
108-
109- li_stream_simple_socket_close (stream , aborted );
110- li_iostream_release (stream );
111-
112- {
113- liStream * raw_out = con -> con_sock .raw_out , * raw_in = con -> con_sock .raw_in ;
114- con -> con_sock .raw_out = con -> con_sock .raw_in = NULL ;
115- if (NULL != raw_out ) { li_stream_reset (raw_out ); li_stream_release (raw_out ); }
116- if (NULL != raw_in ) { li_stream_reset (raw_in ); li_stream_release (raw_in ); }
117- }
118- }
119-
120- static liThrottleState * simple_tcp_throttle_out (liConnection * con ) {
121- simple_tcp_connection * data = con -> con_sock .data ;
122- if (NULL == data ) return NULL ;
123- if (NULL == data -> sock_stream -> throttle_out ) data -> sock_stream -> throttle_out = li_throttle_new ();
124- return data -> sock_stream -> throttle_out ;
125- }
126-
127- static liThrottleState * simple_tcp_throttle_in (liConnection * con ) {
128- simple_tcp_connection * data = con -> con_sock .data ;
129- if (NULL == data ) return NULL ;
130- if (NULL == data -> sock_stream -> throttle_in ) data -> sock_stream -> throttle_in = li_throttle_new ();
131- return data -> sock_stream -> throttle_in ;
132- }
133-
134- static const liConnectionSocketCallbacks simple_tcp_cbs = {
135- simple_tcp_finished ,
136- simple_tcp_throttle_out ,
137- simple_tcp_throttle_in
138- };
139-
140- static gboolean simple_tcp_new (liConnection * con , int fd ) {
141- simple_tcp_connection * data = g_slice_new0 (simple_tcp_connection );
142- data -> sock_stream = li_iostream_new (con -> wrk , fd , simple_tcp_io_cb , data );
143- li_connection_simple_tcp_init (& data -> simple_tcp_state );
144- data -> con = con ;
145- con -> con_sock .data = data ;
146- con -> con_sock .callbacks = & simple_tcp_cbs ;
147- con -> con_sock .raw_out = & data -> sock_stream -> stream_out ;
148- con -> con_sock .raw_in = & data -> sock_stream -> stream_in ;
149- li_stream_acquire (con -> con_sock .raw_out );
150- li_stream_acquire (con -> con_sock .raw_in );
151-
152- return TRUE;
153- }
154-
155-
156-
157-
158-
159-
1607static void con_iostream_close (liConnection * con ) { /* force close */
1618 if (con -> con_sock .callbacks ) {
1629 con -> info .aborted = TRUE;
@@ -176,7 +23,6 @@ static void con_iostream_shutdown(liConnection *con) { /* (try) regular shutdown
17623 LI_FORCE_ASSERT (NULL == con -> con_sock .data );
17724}
17825
179-
18026static void connection_close (liConnection * con );
18127static void li_connection_reset_keep_alive (liConnection * con );
18228
@@ -535,7 +381,7 @@ void li_connection_start(liConnection *con, liSocketAddress remote_addr, int s,
535381 return ;
536382 }
537383 } else {
538- simple_tcp_new (con , s );
384+ li_connection_http_new (con , s );
539385 }
540386
541387 LI_FORCE_ASSERT (NULL != con -> con_sock .raw_in || NULL != con -> con_sock .raw_out );
0 commit comments