2 * HTTP GET/PUT IO engine
4 * IO engine to perform HTTP(S) GET/PUT requests via libcurl-easy.
6 * Copyright (C) 2018 SUSE LLC
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License,
10 * version 2 as published by the Free Software Foundation..
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 #include <curl/curl.h>
26 #include <openssl/hmac.h>
27 #include <openssl/sha.h>
29 #include "../optgroup.h"
49 struct http_curl_stream {
55 static struct fio_option options[] = {
60 .help = "Enable https",
61 .off1 = offsetof(struct http_options, https),
63 .category = FIO_OPT_C_ENGINE,
64 .group = FIO_OPT_G_HTTP,
69 .type = FIO_OPT_STR_STORE,
70 .help = "Hostname (S3 bucket)",
71 .off1 = offsetof(struct http_options, host),
73 .category = FIO_OPT_C_ENGINE,
74 .group = FIO_OPT_G_HTTP,
79 .type = FIO_OPT_STR_STORE,
80 .help = "HTTP user name",
81 .off1 = offsetof(struct http_options, user),
82 .category = FIO_OPT_C_ENGINE,
83 .group = FIO_OPT_G_HTTP,
88 .type = FIO_OPT_STR_STORE,
89 .help = "HTTP password",
90 .off1 = offsetof(struct http_options, pass),
91 .category = FIO_OPT_C_ENGINE,
92 .group = FIO_OPT_G_HTTP,
95 .name = "http_s3_key",
96 .lname = "S3 secret key",
97 .type = FIO_OPT_STR_STORE,
98 .help = "S3 secret key",
99 .off1 = offsetof(struct http_options, s3_key),
101 .category = FIO_OPT_C_ENGINE,
102 .group = FIO_OPT_G_HTTP,
105 .name = "http_s3_keyid",
106 .lname = "S3 key id",
107 .type = FIO_OPT_STR_STORE,
109 .off1 = offsetof(struct http_options, s3_keyid),
111 .category = FIO_OPT_C_ENGINE,
112 .group = FIO_OPT_G_HTTP,
115 .name = "http_s3_region",
116 .lname = "S3 region",
117 .type = FIO_OPT_STR_STORE,
119 .off1 = offsetof(struct http_options, s3_region),
121 .category = FIO_OPT_C_ENGINE,
122 .group = FIO_OPT_G_HTTP,
126 .lname = "S3 extensions",
127 .type = FIO_OPT_BOOL,
128 .help = "Whether to enable S3 specific headers",
129 .off1 = offsetof(struct http_options, s3),
131 .category = FIO_OPT_C_ENGINE,
132 .group = FIO_OPT_G_HTTP,
135 .name = "http_verbose",
136 .lname = "CURL verbosity",
138 .help = "increase http engine verbosity",
139 .off1 = offsetof(struct http_options, verbose),
141 .category = FIO_OPT_C_ENGINE,
142 .group = FIO_OPT_G_HTTP,
149 static char *_aws_uriencode(const char *uri)
151 size_t bufsize = 1024;
152 char *r = malloc(bufsize);
155 const char *hex = "0123456789ABCDEF";
158 log_err("malloc failed\n");
163 for (i = 0; (c = uri[i]); i++) {
165 log_err("encoding the URL failed\n");
169 if ( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')
170 || (c >= '0' && c <= '9') || c == '_' || c == '-'
171 || c == '~' || c == '.' || c == '/')
175 r[n++] = hex[(c >> 4 ) & 0xF];
176 r[n++] = hex[c & 0xF];
183 static char *_conv_hex(const unsigned char *p, size_t len)
187 const char *hex = "0123456789abcdef";
188 r = malloc(len * 2 + 1);
190 for (i = 0; i < len; i++) {
191 r[n++] = hex[(p[i] >> 4 ) & 0xF];
192 r[n++] = hex[p[i] & 0xF];
199 static char *_gen_hex_sha256(const char *p, size_t len)
201 unsigned char hash[SHA256_DIGEST_LENGTH];
203 SHA256((unsigned char*)p, len, hash);
204 return _conv_hex(hash, SHA256_DIGEST_LENGTH);
207 static void _hmac(unsigned char *md, void *key, int key_len, char *data) {
208 #ifndef CONFIG_HAVE_OPAQUE_HMAC_CTX
212 unsigned int hmac_len;
214 #ifdef CONFIG_HAVE_OPAQUE_HMAC_CTX
215 ctx = HMAC_CTX_new();
219 HMAC_Init_ex(ctx, key, key_len, EVP_sha256(), NULL);
220 HMAC_Update(ctx, (unsigned char*)data, strlen(data));
221 HMAC_Final(ctx, md, &hmac_len);
222 #ifdef CONFIG_HAVE_OPAQUE_HMAC_CTX
225 HMAC_CTX_cleanup(ctx);
229 static int _curl_trace(CURL *handle, curl_infotype type,
230 char *data, size_t size,
234 (void)handle; /* prevent compiler warning */
239 fprintf(stderr, "== Info: %s", data);
241 case CURLINFO_SSL_DATA_OUT:
242 case CURLINFO_SSL_DATA_IN:
245 case CURLINFO_HEADER_OUT:
246 text = "=> Send header";
248 case CURLINFO_DATA_OUT:
249 text = "=> Send data";
251 case CURLINFO_HEADER_IN:
252 text = "<= Recv header";
254 case CURLINFO_DATA_IN:
255 text = "<= Recv data";
259 log_info("%s: %s", text, data);
263 /* https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-header-based-auth.html
264 * https://docs.aws.amazon.com/AmazonS3/latest/API/sig-v4-authenticating-requests.html#signing-request-intro
266 static void _add_aws_auth_header(CURL *curl, struct curl_slist *slist, struct http_options *o,
267 int op, const char *uri, char *buf, size_t len)
276 char *uri_encoded = NULL;
279 char *signature = NULL;
280 const char *service = "s3";
281 const char *aws = "aws4_request";
282 unsigned char md[SHA256_DIGEST_LENGTH];
284 time_t t = time(NULL);
285 struct tm *gtm = gmtime(&t);
287 strftime (date_short, sizeof(date_short), "%Y%m%d", gtm);
288 strftime (date_iso, sizeof(date_iso), "%Y%m%dT%H%M%SZ", gtm);
289 uri_encoded = _aws_uriencode(uri);
291 if (op == DDIR_WRITE) {
292 dsha = _gen_hex_sha256(buf, len);
293 sprintf(method, "PUT");
295 /* DDIR_READ && DDIR_TRIM supply an empty body */
297 sprintf(method, "GET");
299 sprintf(method, "DELETE");
300 dsha = _gen_hex_sha256("", 0);
303 /* Create the canonical request first */
304 snprintf(creq, sizeof(creq),
309 "x-amz-content-sha256:%s\n"
312 "host;x-amz-content-sha256;x-amz-date\n"
315 , uri_encoded, o->host, dsha, date_iso, dsha);
317 csha = _gen_hex_sha256(creq, strlen(creq));
318 snprintf(sts, sizeof(sts), "AWS4-HMAC-SHA256\n%s\n%s/%s/%s/%s\n%s",
319 date_iso, date_short, o->s3_region, service, aws, csha);
321 snprintf((char *)dkey, sizeof(dkey), "AWS4%s", o->s3_key);
322 _hmac(md, dkey, strlen(dkey), date_short);
323 _hmac(md, md, SHA256_DIGEST_LENGTH, o->s3_region);
324 _hmac(md, md, SHA256_DIGEST_LENGTH, (char*) service);
325 _hmac(md, md, SHA256_DIGEST_LENGTH, (char*) aws);
326 _hmac(md, md, SHA256_DIGEST_LENGTH, sts);
328 signature = _conv_hex(md, SHA256_DIGEST_LENGTH);
330 /* Surpress automatic Accept: header */
331 slist = curl_slist_append(slist, "Accept:");
333 snprintf(s, sizeof(s), "x-amz-content-sha256: %s", dsha);
334 slist = curl_slist_append(slist, s);
336 snprintf(s, sizeof(s), "x-amz-date: %s", date_iso);
337 slist = curl_slist_append(slist, s);
339 snprintf(s, sizeof(s), "Authorization: AWS4-HMAC-SHA256 Credential=%s/%s/%s/s3/aws4_request,"
340 "SignedHeaders=host;x-amz-content-sha256;x-amz-date,Signature=%s",
341 o->s3_keyid, date_short, o->s3_region, signature);
342 slist = curl_slist_append(slist, s);
344 curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
352 static void fio_http_cleanup(struct thread_data *td)
354 struct http_data *http = td->io_ops_data;
357 curl_easy_cleanup(http->curl);
362 static size_t _http_read(void *ptr, size_t size, size_t nmemb, void *stream)
364 struct http_curl_stream *state = stream;
365 size_t len = size * nmemb;
366 /* We're retrieving; nothing is supposed to be read locally */
369 if (len+state->pos > state->max)
370 len = state->max - state->pos;
371 memcpy(ptr, &state->buf[state->pos], len);
376 static size_t _http_write(void *ptr, size_t size, size_t nmemb, void *stream)
378 struct http_curl_stream *state = stream;
379 /* We're just discarding the returned body after a PUT */
383 return CURLE_WRITE_ERROR;
384 if (nmemb + state->pos > state->max)
385 return CURLE_WRITE_ERROR;
386 memcpy(&state->buf[state->pos], ptr, nmemb);
391 static int _http_seek(void *stream, curl_off_t offset, int origin)
393 struct http_curl_stream *state = stream;
394 if (offset < state->max && origin == SEEK_SET) {
396 return CURL_SEEKFUNC_OK;
398 return CURL_SEEKFUNC_FAIL;
401 static enum fio_q_status fio_http_queue(struct thread_data *td,
404 struct http_data *http = td->io_ops_data;
405 struct http_options *o = td->eo;
406 struct http_curl_stream _curl_stream;
407 struct curl_slist *slist = NULL;
414 fio_ro_check(td, io_u);
415 memset(&_curl_stream, 0, sizeof(_curl_stream));
416 snprintf(object, sizeof(object), "%s_%llu_%llu", td->files[0]->file_name, io_u->offset, io_u->xfer_buflen);
417 snprintf(url, sizeof(url), "%s://%s%s", o->https ? "https" : "http", o->host, object);
418 curl_easy_setopt(http->curl, CURLOPT_URL, url);
419 _curl_stream.buf = io_u->xfer_buf;
420 _curl_stream.max = io_u->xfer_buflen;
421 curl_easy_setopt(http->curl, CURLOPT_SEEKDATA, &_curl_stream);
422 curl_easy_setopt(http->curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)io_u->xfer_buflen);
425 _add_aws_auth_header(http->curl, slist, o, io_u->ddir, object,
426 io_u->xfer_buf, io_u->xfer_buflen);
428 if (io_u->ddir == DDIR_WRITE) {
429 curl_easy_setopt(http->curl, CURLOPT_READDATA, &_curl_stream);
430 curl_easy_setopt(http->curl, CURLOPT_WRITEDATA, NULL);
431 curl_easy_setopt(http->curl, CURLOPT_UPLOAD, 1L);
432 res = curl_easy_perform(http->curl);
433 if (res == CURLE_OK) {
434 curl_easy_getinfo(http->curl, CURLINFO_RESPONSE_CODE, &status);
435 if (status == 100 || (status >= 200 && status <= 204))
437 log_err("DDIR_WRITE failed with HTTP status code %ld\n", status);
440 } else if (io_u->ddir == DDIR_READ) {
441 curl_easy_setopt(http->curl, CURLOPT_READDATA, NULL);
442 curl_easy_setopt(http->curl, CURLOPT_WRITEDATA, &_curl_stream);
443 curl_easy_setopt(http->curl, CURLOPT_HTTPGET, 1L);
444 res = curl_easy_perform(http->curl);
445 if (res == CURLE_OK) {
446 curl_easy_getinfo(http->curl, CURLINFO_RESPONSE_CODE, &status);
449 else if (status == 404) {
450 /* Object doesn't exist. Pretend we read
452 memset(io_u->xfer_buf, 0, io_u->xfer_buflen);
455 log_err("DDIR_READ failed with HTTP status code %ld\n", status);
458 } else if (io_u->ddir == DDIR_TRIM) {
459 curl_easy_setopt(http->curl, CURLOPT_HTTPGET, 1L);
460 curl_easy_setopt(http->curl, CURLOPT_CUSTOMREQUEST, "DELETE");
461 curl_easy_setopt(http->curl, CURLOPT_INFILESIZE_LARGE, 0);
462 curl_easy_setopt(http->curl, CURLOPT_READDATA, NULL);
463 curl_easy_setopt(http->curl, CURLOPT_WRITEDATA, NULL);
464 res = curl_easy_perform(http->curl);
465 if (res == CURLE_OK) {
466 curl_easy_getinfo(http->curl, CURLINFO_RESPONSE_CODE, &status);
467 if (status == 200 || status == 202 || status == 204 || status == 404)
469 log_err("DDIR_TRIM failed with HTTP status code %ld\n", status);
474 log_err("WARNING: Only DDIR_READ/DDIR_WRITE/DDIR_TRIM are supported!\n");
478 td_verror(td, io_u->error, "transfer");
480 curl_slist_free_all(slist);
481 return FIO_Q_COMPLETED;
484 static struct io_u *fio_http_event(struct thread_data *td, int event)
486 /* sync IO engine - never any outstanding events */
490 int fio_http_getevents(struct thread_data *td, unsigned int min,
491 unsigned int max, const struct timespec *t)
493 /* sync IO engine - never any outstanding events */
497 static int fio_http_setup(struct thread_data *td)
499 struct http_data *http = NULL;
500 struct http_options *o = td->eo;
502 /* allocate engine specific structure to deal with libhttp. */
503 http = calloc(1, sizeof(*http));
505 log_err("calloc failed.\n");
509 http->curl = curl_easy_init();
511 curl_easy_setopt(http->curl, CURLOPT_VERBOSE, 1L);
513 curl_easy_setopt(http->curl, CURLOPT_DEBUGFUNCTION, &_curl_trace);
514 curl_easy_setopt(http->curl, CURLOPT_NOPROGRESS, 1L);
515 curl_easy_setopt(http->curl, CURLOPT_FOLLOWLOCATION, 1L);
516 curl_easy_setopt(http->curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS);
517 curl_easy_setopt(http->curl, CURLOPT_READFUNCTION, _http_read);
518 curl_easy_setopt(http->curl, CURLOPT_WRITEFUNCTION, _http_write);
519 curl_easy_setopt(http->curl, CURLOPT_SEEKFUNCTION, _http_seek);
520 if (o->user && o->pass) {
521 curl_easy_setopt(http->curl, CURLOPT_USERNAME, o->user);
522 curl_easy_setopt(http->curl, CURLOPT_PASSWORD, o->pass);
523 curl_easy_setopt(http->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
526 td->io_ops_data = http;
528 /* Force single process mode. */
529 td->o.use_thread = 1;
533 fio_http_cleanup(td);
537 static int fio_http_open(struct thread_data *td, struct fio_file *f)
541 static int fio_http_invalidate(struct thread_data *td, struct fio_file *f)
546 static struct ioengine_ops ioengine = {
548 .version = FIO_IOOPS_VERSION,
549 .flags = FIO_DISKLESSIO,
550 .setup = fio_http_setup,
551 .queue = fio_http_queue,
552 .getevents = fio_http_getevents,
553 .event = fio_http_event,
554 .cleanup = fio_http_cleanup,
555 .open_file = fio_http_open,
556 .invalidate = fio_http_invalidate,
558 .option_struct_size = sizeof(struct http_options),
561 static void fio_init fio_http_register(void)
563 register_ioengine(&ioengine);
566 static void fio_exit fio_http_unregister(void)
568 unregister_ioengine(&ioengine);