Skip to content

Commit a147553

Browse files
committed
[connection] split tcp/unix stream and plain http handling into separate files
Change-Id: I4f5cdb9ac2fe5157e15840b94795f3d2d0320101
1 parent e46142a commit a147553

File tree

5 files changed

+159
-155
lines changed

5 files changed

+159
-155
lines changed

include/lighttpd/connection.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ INLINE void li_connection_simple_tcp_init(liConnectionSimpleTcpState *state);
135135
*/
136136
LI_API void li_connection_simple_tcp(liConnection **pcon, liIOStream *stream, liConnectionSimpleTcpState *state, liIOStreamEvent event);
137137

138+
/* default for liServerSocket->new_cb - plain HTTP */
139+
LI_API gboolean li_connection_http_new(liConnection *con, int fd);
140+
138141
/******************************************************/
139142

140143
/********************

src/main/connection.c

Lines changed: 1 addition & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,9 @@
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-
1607
static 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-
18026
static void connection_close(liConnection *con);
18127
static 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);

src/main/connection_http.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* handle plain HTTP server sockets */
2+
3+
#include <lighttpd/base.h>
4+
#include <lighttpd/throttle.h>
5+
6+
typedef struct simple_tcp_connection simple_tcp_connection;
7+
struct simple_tcp_connection {
8+
liIOStream *sock_stream;
9+
liConnectionSimpleTcpState simple_tcp_state;
10+
liConnection *con;
11+
};
12+
13+
static void simple_tcp_io_cb(liIOStream *stream, liIOStreamEvent event) {
14+
simple_tcp_connection *data = stream->data;
15+
LI_FORCE_ASSERT(NULL != data);
16+
LI_FORCE_ASSERT(NULL == data->con || data == data->con->con_sock.data);
17+
LI_FORCE_ASSERT(NULL == data->sock_stream || stream == data->sock_stream);
18+
19+
li_connection_simple_tcp(&data->con, stream, &data->simple_tcp_state, event);
20+
21+
if (NULL != data->con && data->con->out_has_all_data
22+
&& (NULL == stream->stream_out.out || 0 == stream->stream_out.out->length)) {
23+
li_stream_simple_socket_flush(stream);
24+
li_connection_request_done(data->con);
25+
}
26+
27+
switch (event) {
28+
case LI_IOSTREAM_DESTROY:
29+
LI_FORCE_ASSERT(NULL == data->con);
30+
LI_FORCE_ASSERT(NULL == data->sock_stream);
31+
stream->data = NULL;
32+
g_slice_free(simple_tcp_connection, data);
33+
break;
34+
default:
35+
break;
36+
}
37+
}
38+
39+
static void simple_tcp_finished(liConnection *con, gboolean aborted) {
40+
simple_tcp_connection *data = con->con_sock.data;
41+
liIOStream *stream;
42+
if (NULL == data) return;
43+
44+
data->con = NULL;
45+
con->con_sock.data = NULL;
46+
con->con_sock.callbacks = NULL;
47+
48+
stream = data->sock_stream;
49+
data->sock_stream = NULL;
50+
51+
li_stream_simple_socket_close(stream, aborted);
52+
li_iostream_release(stream);
53+
54+
{
55+
liStream *raw_out = con->con_sock.raw_out, *raw_in = con->con_sock.raw_in;
56+
con->con_sock.raw_out = con->con_sock.raw_in = NULL;
57+
if (NULL != raw_out) { li_stream_reset(raw_out); li_stream_release(raw_out); }
58+
if (NULL != raw_in) { li_stream_reset(raw_in); li_stream_release(raw_in); }
59+
}
60+
}
61+
62+
static liThrottleState* simple_tcp_throttle_out(liConnection *con) {
63+
simple_tcp_connection *data = con->con_sock.data;
64+
if (NULL == data) return NULL;
65+
if (NULL == data->sock_stream->throttle_out) data->sock_stream->throttle_out = li_throttle_new();
66+
return data->sock_stream->throttle_out;
67+
}
68+
69+
static liThrottleState* simple_tcp_throttle_in(liConnection *con) {
70+
simple_tcp_connection *data = con->con_sock.data;
71+
if (NULL == data) return NULL;
72+
if (NULL == data->sock_stream->throttle_in) data->sock_stream->throttle_in = li_throttle_new();
73+
return data->sock_stream->throttle_in;
74+
}
75+
76+
static const liConnectionSocketCallbacks simple_tcp_cbs = {
77+
simple_tcp_finished,
78+
simple_tcp_throttle_out,
79+
simple_tcp_throttle_in
80+
};
81+
82+
gboolean li_connection_http_new(liConnection *con, int fd) {
83+
simple_tcp_connection *data = g_slice_new0(simple_tcp_connection);
84+
data->sock_stream = li_iostream_new(con->wrk, fd, simple_tcp_io_cb, data);
85+
li_connection_simple_tcp_init(&data->simple_tcp_state);
86+
data->con = con;
87+
con->con_sock.data = data;
88+
con->con_sock.callbacks = &simple_tcp_cbs;
89+
con->con_sock.raw_out = &data->sock_stream->stream_out;
90+
con->con_sock.raw_in = &data->sock_stream->stream_in;
91+
li_stream_acquire(con->con_sock.raw_out);
92+
li_stream_acquire(con->con_sock.raw_in);
93+
94+
return TRUE;
95+
}

src/main/connection_tcp.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* basic TCP (or unix domain) stream socket handling */
2+
3+
#include <lighttpd/base.h>
4+
5+
void li_connection_simple_tcp(liConnection **pcon, liIOStream *stream, liConnectionSimpleTcpState *state, liIOStreamEvent event) {
6+
liConnection *con;
7+
goffset transfer_in = 0, transfer_out = 0;
8+
9+
transfer_in = (NULL != stream->stream_in.out) ? stream->stream_in.out->bytes_in : 0;
10+
transfer_out = (NULL != stream->stream_out.out) ? stream->stream_out.out->bytes_out : 0;
11+
12+
li_stream_simple_socket_io_cb_with_buffer(stream, event, &state->read_buffer);
13+
14+
/* li_stream_simple_socket_io_cb_with_buffer might lead to *pcon == NULL */
15+
con = *pcon;
16+
if (NULL != con) {
17+
if (NULL != stream->stream_in.out) {
18+
transfer_in = stream->stream_in.out->bytes_in - transfer_in;
19+
if (transfer_in > 0) {
20+
li_connection_update_io_timeout(con);
21+
li_vrequest_update_stats_in(con->mainvr, transfer_in);
22+
}
23+
}
24+
if (NULL != stream->stream_out.out) {
25+
transfer_out = stream->stream_out.out->bytes_out - transfer_out;
26+
if (transfer_out > 0) {
27+
li_connection_update_io_timeout(con);
28+
li_vrequest_update_stats_out(con->mainvr, transfer_out);
29+
}
30+
}
31+
}
32+
33+
switch (event) {
34+
case LI_IOSTREAM_DESTROY:
35+
li_stream_simple_socket_close(stream, FALSE);
36+
return;
37+
case LI_IOSTREAM_DISCONNECTED_DEST:
38+
if (NULL != stream->stream_in.out && !stream->stream_in.out->is_closed) {
39+
li_stream_simple_socket_close(stream, TRUE);
40+
return;
41+
}
42+
break;
43+
case LI_IOSTREAM_DISCONNECTED_SOURCE:
44+
if (NULL != stream->stream_out.out && !stream->stream_out.out->is_closed) {
45+
li_stream_simple_socket_close(stream, TRUE);
46+
return;
47+
}
48+
break;
49+
default:
50+
break;
51+
}
52+
53+
if ((NULL == stream->stream_in.out || stream->stream_in.out->is_closed) &&
54+
!(NULL == stream->stream_out.out || stream->stream_out.out->is_closed)) {
55+
stream->stream_out.out->is_closed = TRUE;
56+
li_stream_again_later(&stream->stream_out);
57+
}
58+
}

src/main/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ src_shared = [
99
'collect.c',
1010
'condition.c',
1111
'connection.c',
12+
'connection_http.c',
13+
'connection_tcp.c',
1214
'environment.c',
1315
'etag.c',
1416
'filter.c',

0 commit comments

Comments
 (0)