cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob a4c2cb85812a6e3e0cbc371c7b4083a37aa8ab47 11403 bytes (raw)
$ git show wip-1.3:http.c	# shows this blob on the CLI

  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
 
/*
 * Copyright (C) 2012-2013, Eric Wong <normalperson@yhbt.net>
 * License: GPLv3 or later (see COPYING for details)
 */
#include "cmogstored.h"
#include "trace.h"
#include "http.h"

/*
 * On FreeBSD, this disables TCP_NOPUSH momentarily to flush out corked data.
 * This immediately recorks again if we're handling persistent connections,
 * otherwise we leave it uncorked if we're going to close the socket anyways
 */
static void tcp_push(struct mog_fd *mfd, bool recork)
{
	socklen_t len = (socklen_t)sizeof(int);
	int val = 0;
	int rv;

	if (MOG_TCP_NOPUSH == 0)
		return;

	rv = setsockopt(mfd->fd, IPPROTO_TCP, MOG_TCP_NOPUSH, &val, len);

	if (rv == 0 && recork) {
		val = 1;
		setsockopt(mfd->fd, IPPROTO_TCP, MOG_TCP_NOPUSH, &val, len);
	}
	/* deal with errors elsewhere */
}

/* stash any pipelined data for the next round */
static void
http_defer_rbuf(struct mog_http *http, struct mog_rbuf *rbuf, size_t buf_len)
{
	struct mog_rbuf *old = http->rbuf;
	size_t defer_bytes = buf_len - http->_p.buf_off;
	char *src = rbuf->rptr + http->_p.buf_off;

	if (http->_p.skip_rbuf_defer) {
		http->_p.skip_rbuf_defer = 0;
		return;
	}

	assert(http->_p.buf_off >= 0 && "http->_p.buf_off negative");
	assert(defer_bytes <= MOG_RBUF_MAX_SIZE && "defer bytes overflow");

	if (defer_bytes == 0) {
		mog_rbuf_free_and_null(&http->rbuf);
	} else if (old) { /* no allocation needed, reuse existing */
		assert(old == rbuf && "http->rbuf not reused properly");
		memmove(old->rptr, src, defer_bytes);
		old->rsize = defer_bytes;
	} else {
		http->rbuf = mog_rbuf_new(defer_bytes);
		memcpy(http->rbuf->rptr, src, defer_bytes);
		http->rbuf->rsize = defer_bytes;
	}
	http->_p.buf_off = 0;
}

static void
http_process_client(struct mog_fd *mfd, char *buf, size_t buf_len)
{
	struct mog_http *http = &mfd->as.http;

	switch (http->_p.http_method) {
	case MOG_HTTP_METHOD_NONE: assert(0 && "BUG: unset HTTP method");
	case MOG_HTTP_METHOD_GET: mog_http_get_open(mfd, buf); break;
	case MOG_HTTP_METHOD_HEAD: mog_http_get_open(mfd, buf); break;
	case MOG_HTTP_METHOD_DELETE: mog_http_delete(mfd, buf); break;
	case MOG_HTTP_METHOD_MKCOL: mog_http_mkcol(mfd, buf); break;
	case MOG_HTTP_METHOD_PUT: mog_http_put(mfd, buf, buf_len); break;
	}
}

MOG_NOINLINE static void http_close(struct mog_fd *mfd)
{
	struct mog_http *http = &mfd->as.http;

	mog_rbuf_free(http->rbuf);
	assert((http->wbuf == NULL || http->wbuf == MOG_WR_ERROR) &&
	       "would leak http->wbuf on close");

	/*
	 * uncork to avoid ECONNCRESET if we have unread data
	 * (but already wrote a response).  This can help get
	 * the proper error sent to the client if the client is
	 * writing a request that's too big to read and we reset
	 * their connection to save ourselves bandwidth/cycles
	 */
	tcp_push(mfd, false);
	mog_packaddr_free(&http->mpa);

	mog_fd_put(mfd);
}

void mog_http_unlink_ftmp(struct mog_http *http)
{
	struct mog_file *file = &http->forward->as.file;

	if (!file->tmppath)
		return;

	if (mog_unlink(http->svc, file->tmppath) != 0)
		syslog(LOG_ERR, "Failed to unlink %s (in %s): %m",
		       file->tmppath, http->svc->docroot);
}

/* called if epoll/kevent is out-of-space */
void mog_http_drop(struct mog_fd *mfd)
{
	struct mog_http *http = &mfd->as.http;

	assert(http->forward != MOG_IOSTAT);
	if (http->forward) {
		mog_http_unlink_ftmp(http);
		mog_file_close(http->forward);
	}
	http_close(mfd);
}

/* returns true if we can continue queue step, false if not */
static enum mog_next http_wbuf_in_progress(struct mog_http *http)
{
	assert(http->wbuf != MOG_WR_ERROR && "still active after write error");
	switch (mog_tryflush(mog_fd_of(http)->fd, &http->wbuf)) {
	case MOG_WRSTATE_ERR:
		return MOG_NEXT_CLOSE;
	case MOG_WRSTATE_DONE:
		if (!http->_p.persistent) return MOG_NEXT_CLOSE;
		if (http->forward == NULL)
			mog_http_reset(http);
		assert(http->_p.buf_off == 0 && "bad offset");
		return MOG_NEXT_ACTIVE;
	case MOG_WRSTATE_BUSY:
		/* unlikely, we never put anything big in wbuf */
		return MOG_NEXT_WAIT_WR;
	}
	assert(0 && "compiler bug?");
	return MOG_NEXT_CLOSE;
}

static enum mog_next http_forward_in_progress(struct mog_fd *mfd)
{
	enum mog_http_method method = mfd->as.http._p.http_method;

	if (method == MOG_HTTP_METHOD_GET)
		return mog_http_get_in_progress(mfd);

	assert(method == MOG_HTTP_METHOD_PUT && "bad http_method for forward");

	return mog_http_put_in_progress(mfd);
}

static enum mog_next http_queue_step(struct mog_fd *mfd)
{
	struct mog_http *http = &mfd->as.http;
	struct mog_rbuf *rbuf;
	char *buf;
	ssize_t r;
	uint32_t off;
	size_t buf_len = 0;
	enum mog_parser_state state;

	assert(mfd->fd >= 0 && "http fd is invalid");

	if (http->wbuf) return http_wbuf_in_progress(http);
	if (http->forward) return http_forward_in_progress(mfd);

	/* we may have pipelined data in http->rbuf */
	rbuf = http->rbuf ? http->rbuf : mog_rbuf_get(MOG_RBUF_BASE_SIZE);
	buf = rbuf->rptr;
	off = http->_p.buf_off;
	assert(off < rbuf->rcapa && "offset is too big");
	if (http->rbuf) {
		/* request got pipelined, resuming now */
		buf_len = http->rbuf->rsize;
		assert(http->_p.buf_off <= buf_len
			&& "bad offset from pipelining");
		assert(buf_len <= http->rbuf->rcapa && "bad rsize stashed");
		if (http->_p.buf_off < buf_len)
			goto parse;
	}
reread:
	r = read(mfd->fd, buf + off, rbuf->rcapa - off);
	if (r > 0) {
		if (off == 0)
			TRACE(CMOGSTORED_HTTP_REQ_BEGIN(false));

		buf_len = r + off;
parse:
		state = mog_http_parse(http, buf, buf_len);

		switch (state) {
		case MOG_PARSER_ERROR:
			goto err507or400;
		case MOG_PARSER_CONTINUE:
			assert(http->wbuf == NULL &&
			       "tried to write (and failed) with partial req");
			if (http->_p.buf_off >= rbuf->rcapa) {
				rbuf->rsize = buf_len;
				http->rbuf = rbuf = mog_rbuf_grow(rbuf);
				if (!rbuf)
					goto err400;
				buf = rbuf->rptr;
			}
			off = http->_p.buf_off;
			goto reread;
		case MOG_PARSER_DONE:
			http_process_client(mfd, buf, buf_len);
			if (http->wbuf == MOG_WR_ERROR)
				return MOG_NEXT_CLOSE;
			if (http->wbuf) {
				http_defer_rbuf(http, rbuf, buf_len);
				return MOG_NEXT_WAIT_WR;
			} else if (http->forward) {
				http_defer_rbuf(http, rbuf, buf_len);
				return http_forward_in_progress(mfd);
			} else if (!http->_p.persistent) {
				return MOG_NEXT_CLOSE;
			} else {
				/* pipelined request */
				if (buf_len)
					TRACE(CMOGSTORED_HTTP_REQ_BEGIN(true));

				http_defer_rbuf(http, rbuf, buf_len);
				mog_http_reset(http);
			}
			return MOG_NEXT_ACTIVE;
		}
	} else if (r == 0) { /* client shut down */
		TRACE(CMOGSTORED_HTTP_RDCLOSE(buf_len));
		return MOG_NEXT_CLOSE;
	} else {
		switch (errno) {
		case_EAGAIN:
			if (buf_len > 0) {
				if (http->rbuf == NULL)
					http->rbuf = mog_rbuf_detach(rbuf);
				http->rbuf->rsize = buf_len;
			}
			return MOG_NEXT_WAIT_RD;
		case EINTR: goto reread;
		case ECONNRESET:
		case ENOTCONN:
			/* these errors are too common to log, normally */
			TRACE(CMOGSTORED_HTTP_RDERR(buf_len, errno));
			return MOG_NEXT_CLOSE;
		default:
			TRACE(CMOGSTORED_HTTP_RDERR(buf_len, errno));
			syslog(LOG_NOTICE, "http client died: %m");
			return MOG_NEXT_CLOSE;
		}
	}

	assert(0 && "compiler bug?");

err507or400:
	if (errno == ERANGE) {
		mog_http_resp(http, "507 Insufficient Storage", false);
	} else {
err400:
		mog_http_resp(http, "400 Bad Request", false);
	}
	return MOG_NEXT_CLOSE;
}

enum mog_next mog_http_queue_step(struct mog_fd *mfd)
{
	enum mog_next rv = http_queue_step(mfd);

	if (rv == MOG_NEXT_CLOSE)
		http_close(mfd);
	assert(rv != MOG_NEXT_IGNORE &&
	       "refusing to put HTTP client into ignore state");
	return rv;
}

/* called during graceful shutdown instead of mog_http_queue_step */
void mog_http_quit_step(struct mog_fd *mfd)
{
	struct mog_http *http = &mfd->as.http;
	struct mog_queue *q = http->svc->queue;

	/* centralize all queue transitions here: */
	switch (http_queue_step(mfd)) {
	case MOG_NEXT_WAIT_RD:
		if (http->forward || http->rbuf) {
			mog_idleq_push(q, mfd, MOG_QEV_RD);
			return;
		}
		/* fall-through */
	case MOG_NEXT_CLOSE:
		mog_nr_active_at_quit--;
		http_close(mfd);
		return;
	case MOG_NEXT_ACTIVE: mog_activeq_push(q, mfd); return;
	case MOG_NEXT_WAIT_WR: mog_idleq_push(q, mfd, MOG_QEV_WR); return;
	case MOG_NEXT_IGNORE:
		assert(0 && "refused to put HTTP client into ignore state");
	}
}

/* stringify the address for tracers */
static MOG_NOINLINE void
trace_http_accepted(struct mog_fd *mfd)
{
#ifdef HAVE_SYSTEMTAP
	struct mog_packaddr *mpa = &mfd->as.http.mpa;
	struct mog_ni ni;

	mog_nameinfo(mpa, &ni);
	TRACE(CMOGSTORED_HTTP_ACCEPTED(mfd->fd, ni.ni_host, ni.ni_serv));
#endif /* !HAVE_SYSTEMTAP */
}

static void http_post_accept_common(struct mog_fd *mfd, struct mog_svc *svc,
			union mog_sockaddr *msa, socklen_t salen)
{
	struct mog_http *http = &mfd->as.http;

	mog_http_init(http, svc);
	mog_packaddr_init(&http->mpa, msa, salen);

	if (TRACE_ENABLED(CMOGSTORED_HTTP_ACCEPTED))
		trace_http_accepted(mfd);

	mog_idleq_add(svc->queue, mfd, MOG_QEV_RD);
}

/* called immediately after accept(), this initializes the mfd (once) */
void mog_http_post_accept(int fd, struct mog_svc *svc,
			union mog_sockaddr *msa, socklen_t salen)
{
	struct mog_fd *mfd = mog_fd_init(fd, MOG_FD_TYPE_HTTP);

	http_post_accept_common(mfd, svc, msa, salen);
}

/* called immediately after accept(), this initializes the mfd (once) */
void mog_httpget_post_accept(int fd, struct mog_svc *svc,
			union mog_sockaddr *msa, socklen_t salen)
{
	struct mog_fd *mfd = mog_fd_init(fd, MOG_FD_TYPE_HTTPGET);

	http_post_accept_common(mfd, svc, msa, salen);
}

/*
 * returns a NUL-terminated HTTP path from the rbuf pointer
 * returns NUL if we got a bad path.
 */
char *mog_http_path(struct mog_http *http, char *buf)
{
	char *path = buf + http->_p.path_tip;
	size_t len = http->_p.path_end - http->_p.path_tip;

	assert(http->_p.path_end > http->_p.path_tip
		&& "bad HTTP path from parser");

	if (! mog_valid_path(path, len))
		return NULL;

	if (http->_p.http_method == MOG_HTTP_METHOD_PUT) {
		if (!mog_valid_put_path(path, len)) {
			errno = EINVAL;
			return NULL;
		}
	}

	path[len] = '\0';

	return path;
}


/* TODO: see if the iovec overheads of writev() is even worth it... */
void
mog_http_resp0(
	struct mog_http *http,
	struct iovec *status,
	bool alive)
{
	struct iovec iov;
	struct mog_now *now;
	char *dst = iov.iov_base = mog_fsbuf_get(&iov.iov_len);

	assert(status->iov_len * 2 + 1024 < iov.iov_len && "fsbuf too small");

#define CPY(str) mempcpy(dst, (str),(sizeof(str)-1))
	dst = CPY("HTTP/1.1 ");
	dst = mempcpy(dst, status->iov_base, status->iov_len);
	dst = CPY("\r\nStatus: ");
	dst = mempcpy(dst, status->iov_base, status->iov_len);
	dst = CPY("\r\nDate: ");
	now = mog_now();
	dst = mempcpy(dst, now->httpdate, sizeof(now->httpdate)-1);
	if (alive && http->_p.persistent) {
		dst = CPY("\r\nContent-Length: 0"
			"\r\nContent-Type: text/plain"
			"\r\nConnection: keep-alive\r\n\r\n");
	} else {
		http->_p.persistent = 0;
		dst = CPY("\r\nContent-Length: 0"
			"\r\nContent-Type: text/plain"
			"\r\nConnection: close\r\n\r\n");
	}
	iov.iov_len = dst - (char *)iov.iov_base;
	assert(http->wbuf == NULL && "tried to write with wbuf");

	http->wbuf = mog_trywritev(mog_fd_of(http)->fd, &iov, 1);
}

/* call whenever we're ready to read the next HTTP request */
void mog_http_reset(struct mog_http *http)
{
	tcp_push(mog_fd_of(http), true);
	mog_http_reset_parser(http);
}

git clone https://yhbt.net/cmogstored.git