cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob fc4bfd5925a6ce26294ca157492e487c0423ab02 4696 bytes (raw)
$ git show valgrind-fixes:http_parser.rl	# 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
 
/*
 * Copyright (C) 2012-2013, Eric Wong <normalperson@yhbt.net>
 * License: GPLv3 or later (see COPYING for details)
 */
#include "cmogstored.h"
#include "http_util.h"

static bool length_incr(off_t *len, unsigned c)
{
	off_t prev = *len;

	*len *= 10;
	*len += c - '0';

	if (*len >= prev)
		return true;

	errno = ERANGE;
	*len = -1;

	return false;
}

%%{
	machine http_parser;
	include http_common "http_common.rl";
	include path_parser "path_parser.rl";

	ignored_header := header_name ':' sep header_value eor @ {
		fgoto more_headers;
	};

	GET = "GET "> { http->_p.http_method = MOG_HTTP_METHOD_GET; };
	HEAD = "HEAD "> { http->_p.http_method = MOG_HTTP_METHOD_HEAD; };
	PUT = "PUT "> { http->_p.http_method = MOG_HTTP_METHOD_PUT; };
	DELETE = "DELETE "> { http->_p.http_method = MOG_HTTP_METHOD_DELETE; };
	MKCOL = "MKCOL "> { http->_p.http_method = MOG_HTTP_METHOD_MKCOL; };

	# no HTTP/0.9 for now, sorry (not :P)
	req_line = (HEAD|GET|PUT|DELETE|MKCOL)
		("http://" [^/]+)?
		'/'*(mog_path) > { http->_p.path_tip = to_u8(fpc - buf); }
		# TODO: maybe folks use query string/fragments for logging...
		(" HTTP/1.") > { http->_p.path_end = to_u8(fpc - buf); }
		('0'|'1'> { http->_p.persistent = 1; }) '\r'LF;

	content_length = "Content-Length:"i sep
		(digit+) $ {
			if (!length_incr(&http->_p.content_len, fc))
				fbreak;
		}
		$! { errno = EINVAL; fbreak; }
		eor;
	content_range = "Content-Range:"i sep "bytes"LWS+
		(digit+) $ {
			if (!length_incr(&http->_p.range_beg, fc))
				fbreak;
		}
		$! { errno = EINVAL; fbreak; }
		"-"
		(digit+) $ {
			if (!length_incr(&http->_p.range_end, fc))
				fbreak;
		}
		$! { errno = EINVAL; fbreak; }
		"/*"
		eor > { http->_p.has_content_range = 1; };
	range = "Range:"i sep (
			"bytes=" > {
				http->_p.range_beg = http->_p.range_end = -1;
			}
			(digit*) $ {
				if (http->_p.range_beg < 0)
					http->_p.range_beg = 0;
				if (!length_incr(&http->_p.range_beg, fc))
					fbreak;
			}
			'-'
			(digit*) $ {
				if (http->_p.range_end < 0)
					http->_p.range_end = 0;
				if (!length_incr(&http->_p.range_end, fc))
					fbreak;
			}
                ) $! { errno = EINVAL; fbreak; }
		eor @ { http->_p.has_range = 1; };
	transfer_encoding_chunked = "Transfer-Encoding:"i sep
		"chunked"i eor > { http->_p.chunked = 1; };
	trailer = "Trailer:"i sep
		(("Content-MD5"i @ { http->_p.has_md5 = 1; })
		 | header_name | ',')
		eor;
	connection = "Connection:"i sep
		(("close"i @ { http->_p.persistent = 0; }) |
		 ("keep-alive"i @ { http->_p.persistent = 1; })) eor;
	header_line =
		( content_length |
		  transfer_encoding_chunked |
		  trailer |
		  range |
		  content_range |
		  content_md5 |
		  connection ) $!
		{
			assert(http->_p.line_end > 0 &&
			       "no previous request/header line");
			assert(buf[http->_p.line_end] == '\n' &&
			       "bad http->_p.line_end");
			p = buf + http->_p.line_end + 1;
			assert(p <= pe && "overflow");
			fgoto ignored_header;
		};
	headers = header_line* '\r''\n' > { really_done = 1; fbreak; };
	more_headers := headers;
	main := req_line headers;
}%%

%% write data;

void mog_http_reset_parser(struct mog_http *http)
{
	int cs;

	%% write init;
	http->cs = cs;
	memset(&http->_p, 0, sizeof(http->_p));

	if (http->rbuf) /* already pipelined */
		http->_p.persist_client_at_start = 1;
	else
		/*
		 * we need to know persist_client when we start reading
		 * the request because we do not want to break pipelined
		 * requests
		 */
		http->_p.persist_client_at_start = http->svc->persist_client;

	/* these should probably be in mog_http_init */
	http->forward = NULL;
	http->wbuf = NULL;
}

void mog_http_init(struct mog_http *http, struct mog_svc *svc)
{
	http->svc = svc;
	http->rbuf = NULL;
	mog_http_reset_parser(http);
}

enum mog_parser_state
mog_http_parse(struct mog_http *http, char *buf, size_t len)
{
	char *p, *pe, *eof = NULL;
	int cs = http->cs;
	int really_done = 0;
	size_t off = http->_p.buf_off;
	uint32_t *mog_devid = &http->_p.mog_devid;

	assert(http->wbuf == NULL && "unwritten data in buffer");
	assert(off <= len && "http offset past end of buffer");

	p = buf + off;
	pe = buf + len;

	assert((void *)(pe - p) == (void *)(len - off) &&
	       "pointers aren't same distance");

	errno = 0;
	%% write exec;

	if (really_done)
		cs = http_parser_first_final;

	http->cs = cs;
	http->_p.buf_off = p - buf;

	if (cs == http_parser_error || errno)
		return MOG_PARSER_ERROR;

	assert(p <= pe && "buffer overflow after http parse");
	assert(http->_p.buf_off <= len && "offset longer than len");

	if (http->cs == http_parser_first_final) {
		http->_p.persistent &= http->_p.persist_client_at_start;
		return MOG_PARSER_DONE;
	}
	return MOG_PARSER_CONTINUE;
}

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