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
|
/*
* uhttpd - Tiny single-threaded httpd
*
* Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
* Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
*
* 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 <signal.h>
#include "uhttpd.h"
void uh_relay_free(struct relay *r)
{
if (!r->cl)
return;
if (r->proc.pending)
kill(r->proc.pid, SIGKILL);
uloop_process_delete(&r->proc);
ustream_free(&r->sfd.stream);
close(r->sfd.fd.fd);
r->cl = NULL;
}
void uh_relay_close(struct relay *r, int ret)
{
struct ustream *us = &r->sfd.stream;
if (!us->notify_read)
return;
us->notify_read = NULL;
us->notify_write = NULL;
us->notify_state = NULL;
if (r->close)
r->close(r, ret);
}
static void relay_error(struct relay *r)
{
struct ustream *s = &r->sfd.stream;
int len;
s->eof = true;
ustream_get_read_buf(s, &len);
if (len)
ustream_consume(s, len);
ustream_state_change(s);
}
static void relay_process_headers(struct relay *r)
{
struct ustream *s = &r->sfd.stream;
char *buf, *newline;
int len;
if (!r->header_cb)
return;
while (r->header_cb) {
int line_len;
char *val;
buf = ustream_get_read_buf(s, &len);
newline = strchr(buf, '\n');
if (!newline)
break;
line_len = newline + 1 - buf;
if (newline > buf && newline[-1] == '\r')
newline--;
*newline = 0;
if (newline == buf) {
r->header_cb = NULL;
if (r->header_end)
r->header_end(r);
ustream_consume(s, line_len);
break;
}
val = uh_split_header(buf);
if (!val) {
relay_error(r);
return;
}
r->header_cb(r, buf, val);
ustream_consume(s, line_len);
}
}
static void relay_read_cb(struct ustream *s, int bytes)
{
struct relay *r = container_of(s, struct relay, sfd.stream);
struct client *cl = r->cl;
struct ustream *us = cl->us;
char *buf;
int len;
relay_process_headers(r);
if (r->header_cb) {
/*
* if eof, ensure that remaining data is discarded, so the
* state change cb will tear down the stream
*/
if (s->eof)
relay_error(r);
return;
}
if (!s->eof && ustream_pending_data(us, true)) {
ustream_set_read_blocked(s, true);
return;
}
buf = ustream_get_read_buf(s, &len);
uh_chunk_write(cl, buf, len);
ustream_consume(s, len);
}
static void relay_close_if_done(struct relay *r)
{
struct ustream *s = &r->sfd.stream;
if (!s->eof || ustream_pending_data(s, false))
return;
uh_relay_close(r, r->ret);
}
static void relay_state_cb(struct ustream *s)
{
struct relay *r = container_of(s, struct relay, sfd.stream);
if (r->process_done)
relay_close_if_done(r);
}
static void relay_proc_cb(struct uloop_process *proc, int ret)
{
struct relay *r = container_of(proc, struct relay, proc);
r->process_done = true;
r->ret = ret;
relay_close_if_done(r);
}
void uh_relay_open(struct client *cl, struct relay *r, int fd, int pid)
{
struct ustream *us = &r->sfd.stream;
r->cl = cl;
ustream_fd_init(&r->sfd, fd);
us->notify_read = relay_read_cb;
us->notify_state = relay_state_cb;
us->string_data = true;
r->proc.pid = pid;
r->proc.cb = relay_proc_cb;
uloop_process_add(&r->proc);
}
|