Repository navigation
Expand file tree
/
Copy pathcorHttpStream.c
More file actions
285 lines (221 loc) · 6.79 KB
/
Copy pathcorHttpStream.c
File metadata and controls
285 lines (221 loc) · 6.79 KB
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
//
// FILE corHttpStream.c
//
// Copyright 2026 Seamware
// SPDX-License-Identifier: Apache-2.0
//
// Bodies that are not in memory: a file, sent from the file (corHttpResponseFile), and a body written
// over time by the caller, from any thread (corHttpResponseStream).
//
// The loop writes both - after the headers, as the socket takes the bytes (corHttpServer.c, bodyPump).
// What is here is the caller's side of it, and the stream's bookkeeping.
//
// A STREAM'S WRITER NEVER TOUCHES THE CONNECTION. It holds the stream; the stream points at the
// connection only for the loop. A write copies the bytes into the stream under its mutex and, if the
// stream is not queued already, queues it on the loop's stream queue and pokes the loop's eventfd - the
// same wake-up corHttpResume uses, and for the same reason: the socket is the loop's, and two threads
// writing one response is how two answers end up interleaved.
//
#include <stdlib.h> // malloc, realloc, free
#include <string.h> // memcpy, strcmp
#include <unistd.h> // write, close
#include "corHttp/CorHttp.h" // CorHttpConn, CorHttpServer
#include "corHttp/corHttpInternal.h" // Own interface
// -----------------------------------------------------------------------------
//
// corHttpResponseFile -
//
void corHttpResponseFile(CorHttpConn* connP, int fd, int64_t offset, int64_t length)
{
if (connP->fileFd != -1)
close(connP->fileFd);
connP->fileFd = fd;
connP->fileOffset = offset;
connP->fileLeft = (length > 0) ? length : 0;
connP->respBody = NULL;
connP->respBodyLen = 0;
}
// -----------------------------------------------------------------------------
//
// corHttpResponseStream -
//
CorHttpStream* corHttpResponseStream(CorHttpConn* connP)
{
if (connP->streamP != NULL)
return NULL; // one stream per response
CorHttpStream* sP = (CorHttpStream*) calloc(1, sizeof(CorHttpStream));
if (sP == NULL)
return NULL;
pthread_mutex_init(&sP->mutex, NULL);
sP->refs = 2; // the writer and the connection
sP->serverP = connP->serverP;
sP->connP = connP;
sP->chunked = true;
connP->streamP = sP;
connP->respBody = NULL;
connP->respBodyLen = 0;
return sP;
}
// -----------------------------------------------------------------------------
//
// wake - put the stream on its loop's stream queue (once) and wake the loop; called with sP->mutex held
//
// The queue holds a reference of its own: the writer may end, and the connection let go, before the loop
// gets to it.
//
static bool wakeNeeded(CorHttpStream* sP)
{
if (sP->queued == true)
return false;
sP->queued = true;
sP->refs += 1;
return true;
}
static void wake(CorHttpStream* sP)
{
CorHttpServer* serverP = sP->serverP;
pthread_mutex_lock(&serverP->resumeMutex);
sP->next = serverP->streamHead;
serverP->streamHead = sP;
pthread_mutex_unlock(&serverP->resumeMutex);
if (serverP->resumeFd != -1)
{
uint64_t one = 1;
ssize_t ignored = write(serverP->resumeFd, &one, sizeof(one));
(void) ignored; // a full counter means the loop is already awake
}
}
// -----------------------------------------------------------------------------
//
// corHttpStreamWrite -
//
bool corHttpStreamWrite(CorHttpStream* sP, const char* data, int len)
{
if ((sP == NULL) || (len < 0))
return false;
pthread_mutex_lock(&sP->mutex);
if ((sP->gone == true) || (sP->ended == true) || (sP->overflow == true))
{
pthread_mutex_unlock(&sP->mutex);
return false;
}
if (len == 0)
{
pthread_mutex_unlock(&sP->mutex);
return true; // nothing to send - and an empty chunk would END the body
}
bool ok = true;
if (sP->len + len > COR_HTTP_STREAM_PENDING_MAX)
{
sP->overflow = true; // the loop disconnects the client
ok = false;
}
else
{
if (sP->len + len > sP->size)
{
int size = (sP->size == 0) ? 4096 : sP->size;
while (size < sP->len + len)
size *= 2;
char* buf = (char*) realloc(sP->buf, size);
if (buf == NULL)
{
pthread_mutex_unlock(&sP->mutex);
return false;
}
sP->buf = buf;
sP->size = size;
}
memcpy(&sP->buf[sP->len], data, len);
sP->len += len;
}
bool doWake = wakeNeeded(sP);
pthread_mutex_unlock(&sP->mutex);
if (doWake)
wake(sP);
return ok;
}
// -----------------------------------------------------------------------------
//
// corHttpStreamEnd - the last chunk; the writer's reference is dropped
//
void corHttpStreamEnd(CorHttpStream* sP)
{
if (sP == NULL)
return;
pthread_mutex_lock(&sP->mutex);
sP->ended = true;
bool doWake = (sP->gone == false) && wakeNeeded(sP);
pthread_mutex_unlock(&sP->mutex);
if (doWake)
wake(sP);
corHttpStreamUnref(sP);
}
// -----------------------------------------------------------------------------
//
// corHttpStreamTake -
//
char* corHttpStreamTake(CorHttpStream* sP, int* lenP, bool* endedP, bool* overflowP)
{
pthread_mutex_lock(&sP->mutex);
char* buf = sP->buf;
*lenP = sP->len;
*endedP = sP->ended;
*overflowP = sP->overflow;
sP->buf = NULL;
sP->len = 0;
sP->size = 0;
pthread_mutex_unlock(&sP->mutex);
return buf;
}
// -----------------------------------------------------------------------------
//
// corHttpStreamUnref -
//
void corHttpStreamUnref(CorHttpStream* sP)
{
pthread_mutex_lock(&sP->mutex);
int refs = --sP->refs;
pthread_mutex_unlock(&sP->mutex);
if (refs > 0)
return;
pthread_mutex_destroy(&sP->mutex);
free(sP->buf);
free(sP);
}
// -----------------------------------------------------------------------------
//
// corHttpBodyBusy -
//
bool corHttpBodyBusy(CorHttpConn* connP)
{
if (connP->fileFd != -1)
return true;
return (connP->streamP != NULL) && (connP->streamP->started == true);
}
// -----------------------------------------------------------------------------
//
// corHttpBodyRelease - the connection lets go of its file and its stream
//
// A stream: refused to its writer from now on (a write says false), and the connection's reference
// dropped. On the loop's thread, or on whichever thread owns a suspended connection.
//
void corHttpBodyRelease(CorHttpConn* connP)
{
if (connP->fileFd != -1)
{
close(connP->fileFd);
connP->fileFd = -1;
}
connP->fileOffset = 0;
connP->fileLeft = 0;
CorHttpStream* sP = connP->streamP;
if (sP == NULL)
return;
connP->streamP = NULL;
sP->connP = NULL;
pthread_mutex_lock(&sP->mutex);
sP->gone = true;
pthread_mutex_unlock(&sP->mutex);
corHttpStreamUnref(sP);
}