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
|
/* multipart.c -- GA4GH redirection and multipart backend for file streams.
Copyright (C) 2016-2017 Genome Research Ltd.
Author: John Marshall <jm18@sanger.ac.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE. */
#define HTS_BUILDING_LIBRARY // Enables HTSLIB_EXPORT, see htslib/hts_defs.h
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "htslib/kstring.h"
#include "hts_internal.h"
#include "hfile_internal.h"
#ifndef EPROTO
#define EPROTO ENOEXEC
#endif
typedef struct hfile_part {
char *url;
char **headers;
} hfile_part;
typedef struct {
hFILE base;
hfile_part *parts;
size_t nparts, maxparts, current;
hFILE *currentfp;
} hFILE_multipart;
static void free_part(hfile_part *p)
{
free(p->url);
if (p->headers) {
char **hdr;
for (hdr = p->headers; *hdr; hdr++) free(*hdr);
free(p->headers);
}
p->url = NULL;
p->headers = NULL;
}
static void free_all_parts(hFILE_multipart *fp)
{
size_t i;
for (i = 0; i < fp->nparts; i++) free_part(&fp->parts[i]);
free(fp->parts);
}
static ssize_t multipart_read(hFILE *fpv, void *buffer, size_t nbytes)
{
hFILE_multipart *fp = (hFILE_multipart *) fpv;
size_t n;
open_next:
if (fp->currentfp == NULL) {
if (fp->current < fp->nparts) {
const hfile_part *p = &fp->parts[fp->current];
hts_log_debug("Opening part #%zu of %zu: \"%.120s%s\"",
fp->current+1, fp->nparts, p->url,
(strlen(p->url) > 120)? "..." : "");
fp->currentfp = p->headers?
hopen(p->url, "r:",
"httphdr:v", p->headers,
"auth_token_enabled", "false", NULL)
: hopen(p->url, "r:", "auth_token_enabled", "false", NULL);
if (fp->currentfp == NULL) return -1;
}
else return 0; // No more parts, so we're truly at EOF
}
n = fp->currentfp->mobile?
fp->currentfp->backend->read(fp->currentfp, buffer, nbytes)
: hread(fp->currentfp, buffer, nbytes);
if (n == 0) {
// We're at EOF on this part, so set up the next part
hFILE *prevfp = fp->currentfp;
free_part(&fp->parts[fp->current]);
fp->current++;
fp->currentfp = NULL;
if (hclose(prevfp) < 0) return -1;
goto open_next;
}
return n; // Number of bytes read by (or an error from) fp->currentfp
}
static ssize_t multipart_write(hFILE *fpv, const void *buffer, size_t nbytes)
{
errno = EROFS;
return -1;
}
static off_t multipart_seek(hFILE *fpv, off_t offset, int whence)
{
errno = ESPIPE;
return -1;
}
static int multipart_close(hFILE *fpv)
{
hFILE_multipart *fp = (hFILE_multipart *) fpv;
free_all_parts(fp);
if (fp->currentfp) {
if (hclose(fp->currentfp) < 0) return -1;
}
return 0;
}
static const struct hFILE_backend multipart_backend =
{
multipart_read, multipart_write, multipart_seek, NULL, multipart_close
};
// Returns 'v' (valid value), 'i' (invalid; required GA4GH field missing),
// or upon encountering an unexpected token, that token's type.
// Explicit `return '?'` means a JSON parsing error, typically a member key
// that is not a string. An unexpected token may be a valid token that was
// not the type expected for a particular GA4GH field, or it may be '?' or
// '\0' which should be propagated.
static char
parse_ga4gh_body_json(hFILE_multipart *fp, hFILE *json,
kstring_t *b, kstring_t *header)
{
hts_json_token t;
if (hts_json_fnext(json, &t, b) != '{') return t.type;
while (hts_json_fnext(json, &t, b) != '}') {
if (t.type != 's') return '?';
if (strcmp(t.str, "urls") == 0) {
if (hts_json_fnext(json, &t, b) != '[') return t.type;
while (hts_json_fnext(json, &t, b) != ']') {
hfile_part *part;
size_t n = 0, max = 0;
hts_expand(hfile_part, fp->nparts+1, fp->maxparts, fp->parts);
part = &fp->parts[fp->nparts++];
part->url = NULL;
part->headers = NULL;
if (t.type != '{') return t.type;
while (hts_json_fnext(json, &t, b) != '}') {
if (t.type != 's') return '?';
if (strcmp(t.str, "url") == 0) {
if (hts_json_fnext(json, &t, b) != 's') return t.type;
part->url = ks_release(b);
}
else if (strcmp(t.str, "headers") == 0) {
if (hts_json_fnext(json, &t, b) != '{') return t.type;
while (hts_json_fnext(json, &t, header) != '}') {
if (t.type != 's') return '?';
if (hts_json_fnext(json, &t, b) != 's')
return t.type;
kputs(": ", header);
kputs(t.str, header);
n++;
hts_expand(char *, n+1, max, part->headers);
part->headers[n-1] = ks_release(header);
part->headers[n] = NULL;
}
}
else if (hts_json_fskip_value(json, '\0') != 'v')
return '?';
}
if (! part->url) return 'i';
}
}
else if (strcmp(t.str, "format") == 0) {
if (hts_json_fnext(json, &t, b) != 's') return t.type;
hts_log_debug("GA4GH JSON redirection to multipart %s data", t.str);
}
else if (hts_json_fskip_value(json, '\0') != 'v') return '?';
}
return 'v';
}
// Returns 'v' (valid value), 'i' (invalid; required GA4GH field missing),
// or upon encountering an unexpected token, that token's type.
// Explicit `return '?'` means a JSON parsing error, typically a member key
// that is not a string. An unexpected token may be a valid token that was
// not the type expected for a particular GA4GH field, or it may be '?' or
// '\0' which should be propagated.
static char
parse_ga4gh_redirect_json(hFILE_multipart *fp, hFILE *json,
kstring_t *b, kstring_t *header) {
hts_json_token t;
if (hts_json_fnext(json, &t, b) != '{') return t.type;
while (hts_json_fnext(json, &t, b) != '}') {
if (t.type != 's') return '?';
if (strcmp(t.str, "htsget") == 0) {
char ret = parse_ga4gh_body_json(fp, json, b, header);
if (ret != 'v') return ret;
}
else return '?';
}
if (hts_json_fnext(json, &t, b) != '\0') return '?';
return 'v';
}
hFILE *hopen_htsget_redirect(hFILE *hfile, const char *mode)
{
hFILE_multipart *fp;
kstring_t s1 = { 0, 0, NULL }, s2 = { 0, 0, NULL };
char ret;
fp = (hFILE_multipart *) hfile_init(sizeof (hFILE_multipart), mode, 0);
if (fp == NULL) return NULL;
fp->parts = NULL;
fp->nparts = fp->maxparts = 0;
ret = parse_ga4gh_redirect_json(fp, hfile, &s1, &s2);
free(s1.s);
free(s2.s);
if (ret != 'v') {
free_all_parts(fp);
hfile_destroy((hFILE *) fp);
errno = (ret == '?' || ret == '\0')? EPROTO : EINVAL;
return NULL;
}
fp->current = 0;
fp->currentfp = NULL;
fp->base.backend = &multipart_backend;
return &fp->base;
}
|