cmogstored.git  about / heads / tags
alternative mogstored implementation for MogileFS
blob d774dfb8d2a19b045629fd4c2fb1b9f83c3cfa9d 8114 bytes (raw)
$ git show st-wip-broken:http_get.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
 
/*
 * Copyright (C) 2012-2013, Eric Wong <normalperson@yhbt.net>
 * License: GPLv3 or later (see COPYING for details)
 */

#include "cmogstored.h"
#include "http.h"
#if defined(HAVE_SENDFILE)
#  if defined(__linux__)
#    include <sys/sendfile.h>
#  else
/*
 * make BSD sendfile look like Linux for now...
 * we can support SF_NODISKIO later
 */
static ssize_t linux_sendfile(int sockfd, int filefd, off_t *off, size_t count)
{
	int flags = 0;
	off_t sbytes = 0;
	int rc;

	rc = sendfile(filefd, sockfd, *off, count, NULL, &sbytes, flags);
	if (sbytes > 0) {
		*off += sbytes;
		return (ssize_t)sbytes;
	}

	return (ssize_t)rc;
}
#  define sendfile(out_fd, in_fd, offset, count) \
          linux_sendfile((out_fd),(in_fd),(offset),(count))
#  endif
#else
#  include "compat_sendfile.h"
#endif

#define ERR416 "416 Requested Range Not Satisfiable"

/*
 * TODO: refactor this
 *
 * snprintf() usage here is a hot spot in profiling.  Perhaps one day,
 * link-time optimization will be able to work on *printf() functions
 * so we won't hurt code maintainability by optimizing away snprintf()
 * ourselves.  This function is ugly enough already
 */
static off_t http_get_resp_hdr(struct mog_fd *mfd, struct stat *sb)
{
	struct mog_http *http = &mfd->as.http;
	char *modified;
	void *buf;
	size_t len;
	struct mog_now *now = mog_now();
	long long count;
	int rc;

	/* single buffer so we can use MSG_MORE */
	buf = mog_fsbuf_get(&len);
	modified = (char *)buf + len / 2;
	assert((len / 2) > MOG_HTTPDATE_CAPA && "fsbuf too small");
	mog_http_date(modified, MOG_HTTPDATE_CAPA, &sb->st_mtime);

	/* validate ranges */
	if (http->_p.has_range) {
		long long offset;

		if (http->_p.range_end < 0 && http->_p.range_beg < 0)
			goto bad_range;
		if (http->_p.range_beg >= sb->st_size)
			goto bad_range;

		/* bytes=M-N where M > N */
		if (http->_p.range_beg >= 0 && http->_p.range_end >= 0
		    && http->_p.range_beg > http->_p.range_end)
			goto bad_range;

		if (http->_p.range_end < 0) { /* bytes=M- */
			/* bytes starting at M until EOF */
			assert(http->_p.range_beg >= 0 && "should've sent 416");
			offset = (long long)http->_p.range_beg;
			count = (long long)(sb->st_size - offset);
		} else if (http->_p.range_beg < 0) { /* bytes=-N */
			/* last N bytes */
			assert(http->_p.range_end >= 0 && "should've sent 416");
			offset = (long long)(sb->st_size - http->_p.range_end);

			/* serve the entire file if client requested too much */
			if (offset < 0)
				goto resp_200;
			count = (long long)(sb->st_size - offset);
		} else { /* bytes=M-N*/
			assert(http->_p.range_beg >= 0
			       && http->_p.range_end >= 0
			       && "should've sent 416");
			offset = (long long)http->_p.range_beg;

			/* truncate responses to current file size */
			if (http->_p.range_end >= sb->st_size)
				http->_p.range_end = sb->st_size - 1;
			count = (long long)http->_p.range_end + 1 - offset;
		}

		assert(count > 0 && "bad count for 206 response");
		assert(offset >= 0 && "bad offset for 206 response");

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

			file->foff = offset;
			file->fsize = (off_t)(offset + count);
		}

		rc = snprintf(buf, len,
			"HTTP/1.1 206 Partial Content\r\n"
			"Status: 206 Partial Content\r\n"
			"Date: %s\r\n"
			"Last-Modified: %s\r\n"
			"Content-Length: %lld\r\n"
			"Content-Type: application/octet-stream\r\n"
			"Content-Range: bytes %lld-%lld/%lld\r\n"
			"Connection: %s\r\n"
			"\r\n",
			now->httpdate,
			modified,
			count, /* Content-Length */
			offset, offset + count - 1, /* bytes M-N */
			(long long)sb->st_size,
			http->_p.persistent ? "keep-alive" : "close");
	} else {
resp_200:
		count = (long long)sb->st_size;
		rc = snprintf(buf, len,
			"HTTP/1.1 200 OK\r\n"
			"Status: 200 OK\r\n"
			"Date: %s\r\n"
			"Last-Modified: %s\r\n"
			"Content-Length: %lld\r\n"
			"Content-Type: application/octet-stream\r\n"
			"Accept-Ranges: bytes\r\n"
			"Connection: %s\r\n"
			"\r\n",
			now->httpdate,
			modified,
			count,
			http->_p.persistent ? "keep-alive" : "close");
	}

	/* TODO: put down the crack pipe and refactor this */
	if (0) {
bad_range:
		count = 0;
		if (http->forward) {
			mog_file_close(http->forward);
			http->forward = NULL;
		} else {
			assert(http->_p.http_method == MOG_HTTP_METHOD_HEAD
			       && "not HTTP HEAD");
		}
		rc = snprintf(buf, len,
			"HTTP/1.1 " ERR416 "\r\n"
			"Status: " ERR416 "\r\n"
			"Date: %s\r\n"
			"Content-Length: 0\r\n"
			"Content-Type: text/plain\r\n"
			"Content-Range: bytes */%lld\r\n"
			"Connection: %s\r\n"
			"\r\n",
			now->httpdate,
			(long long)sb->st_size,
			http->_p.persistent ? "keep-alive" : "close");
	}

	assert(rc > 0 && rc < len && "we suck at snprintf");
	len = (size_t)rc;
	assert(http->wbuf == NULL && "tried to write to a busy client");

	if (http->_p.http_method == MOG_HTTP_METHOD_HEAD)
		count = 0;

	http->wbuf = mog_trysend(mfd->fd, buf, len, (off_t)count);

	return (off_t)count;
}

void mog_http_get_open(struct mog_fd *mfd, char *buf)
{
	struct mog_http *http = &mfd->as.http;
	struct stat sb;
	struct mog_file *file = NULL;
	char *path = mog_http_path(http, buf);
	off_t len;

	if (!path) goto forbidden; /* path traversal attack */
	assert(http->forward == NULL && "already have http->forward");
	assert(path[0] == '/' && "bad path");

	TRACE(CMOGSTORED_HTTP_REQ_START(mfd->fd,
		http->_p.http_method == MOG_HTTP_METHOD_HEAD ?
		"HEAD" : "GET", path));
	if (path[1] == '\0') { /* keep "mogadm check" happy */
		sb.st_mtime = 0;
		sb.st_size = 0;
	} else if (http->_p.http_method == MOG_HTTP_METHOD_HEAD) {
		if (mog_stat(http->svc, path, &sb) < 0) goto err;
		if (!S_ISREG(sb.st_mode)) goto forbidden;
	} else {
		http->forward = mog_file_open_read(http->svc, path);
		if (http->forward == NULL)
			goto err;

		file = &http->forward->as.file;
		assert(file->path == NULL && "build system bug");
		if (fstat(http->forward->fd, &sb) < 0) {
			PRESERVE_ERRNO( mog_file_close(http->forward) );
			http->forward = NULL;
			goto err;
		}
		if (!S_ISREG(sb.st_mode)) {
			mog_file_close(http->forward);
			http->forward = NULL;
			goto forbidden;
		}
		file->fsize = sb.st_size;
	}

	len = http_get_resp_hdr(mfd, &sb);

	/* http->forward may be NULL even if file is set if we had an error */
	if (http->wbuf == NULL && http->forward) {
		assert(file && "file unset but http->forward is set");

		if (len > (256 * 1024))
			mog_fadv_sequential(http->forward->fd, file->foff, len);
	}
	return;
err:
	switch (errno) {
	case EACCES:
forbidden:
		mog_http_resp(mfd, "403 Forbidden", true);
		return;
	case ENOENT:
		mog_http_resp(mfd, "404 Not Found", true);
		return;
	}
	PRESERVE_ERRNO(do {
		mog_http_resp(mfd, "500 Internal Server Error", true);
	} while(0));
}

enum mog_next mog_http_get_in_progress(struct mog_fd *mfd)
{
	struct mog_http *http = &mfd->as.http;
	struct mog_fd *file_mfd;
	struct mog_file *file;
	ssize_t w;
	off_t count;
	off_t max_sendfile = (mog_ioq_contended() ? 1 : 100) * 1024 * 1024;

	assert(http->wbuf == NULL && "can't serve file with http->wbuf");
	assert(http->forward && http->forward != MOG_IOSTAT && "bad forward");
	file_mfd = http->forward;
	file = &file_mfd->as.file;

	assert(file->fsize >= 0 && "fsize is negative");
	assert(file->foff >= 0 && "foff is negative");
	count = file->fsize - file->foff;
	count = count > max_sendfile ? max_sendfile : count;
	if (count == 0)
		goto done;
retry:
	w = sendfile(mfd->fd, file_mfd->fd, &file->foff, (size_t)count);
	if (w > 0) {
		if (file->foff == file->fsize) goto done;
		return MOG_NEXT_ACTIVE;
	} else if (w < 0) {
		switch (errno) {
		case_EAGAIN: return MOG_NEXT_WAIT_WR;
		case EINTR: goto retry;
		}
		http->_p.persistent = 0;
	} else { /* w == 0 */
		/*
		 * if we can't fulfill the value set by our Content-Length:
		 * header, we must kill the TCP connection
		 */
		http->_p.persistent = 0;
		syslog(LOG_ERR,
		       "sendfile()-d 0 bytes at offset=%lld; file truncated?",
		       (long long)file->foff);
	}
done:
	mog_file_close(http->forward);
	if (http->_p.persistent) {
		mog_http_reset(mfd);
		return MOG_NEXT_ACTIVE;
	}
	return MOG_NEXT_CLOSE;
}

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