[1347] | 1 | //vio_proto.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011 |
---|
[1347] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[1347] | 23 | * |
---|
| 24 | * NOTE for everyone want's to change something and send patches: |
---|
| 25 | * read README and HACKING! There a addition information on |
---|
| 26 | * the license of this document you need to read before you send |
---|
| 27 | * any patches. |
---|
| 28 | * |
---|
| 29 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 30 | * or libpulse*: |
---|
| 31 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 32 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 33 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 34 | */ |
---|
| 35 | |
---|
| 36 | #include "libroar.h" |
---|
| 37 | |
---|
[4832] | 38 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
| 39 | #include <roaraudio/proto_gopher.h> |
---|
| 40 | #endif |
---|
| 41 | |
---|
[1349] | 42 | int roar_vio_proto_init_def (struct roar_vio_defaults * def, char * dstr, int proto, struct roar_vio_defaults * odef) { |
---|
[1439] | 43 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[1349] | 44 | int port = 0; |
---|
| 45 | int ret; |
---|
| 46 | char * ed; |
---|
[4843] | 47 | char * tmp; |
---|
[1349] | 48 | |
---|
| 49 | if ( def == NULL ) |
---|
| 50 | return -1; |
---|
| 51 | |
---|
| 52 | switch (proto) { |
---|
[3078] | 53 | case ROAR_VIO_PROTO_P_HTTP: port = 80; break; |
---|
| 54 | case ROAR_VIO_PROTO_P_GOPHER: port = 70; break; |
---|
| 55 | case ROAR_VIO_PROTO_P_ICY: port = 8000; break; |
---|
[1349] | 56 | default: |
---|
| 57 | return -1; |
---|
| 58 | } |
---|
| 59 | |
---|
[1353] | 60 | if ( dstr == NULL ) |
---|
| 61 | dstr = "//"; |
---|
| 62 | |
---|
[1349] | 63 | if ( roar_vio_dstr_init_defaults(def, ROAR_VIO_DEF_TYPE_SOCKET, O_RDWR, 0644) == -1 ) |
---|
| 64 | return -1; |
---|
| 65 | |
---|
| 66 | if ( roar_vio_socket_init_tcp4_def(def, "localhost", port) == -1 ) |
---|
| 67 | return -1; |
---|
| 68 | |
---|
| 69 | if ( !strncmp(dstr, "//", 2) ) |
---|
| 70 | dstr += 2; |
---|
| 71 | |
---|
| 72 | if ( (ed = strstr(dstr, "/")) != NULL ) |
---|
| 73 | *ed = 0; |
---|
| 74 | |
---|
[4843] | 75 | if ( (tmp = strstr(dstr, "@")) != NULL ) |
---|
| 76 | dstr = tmp + 1; |
---|
| 77 | |
---|
[1616] | 78 | ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags); |
---|
[1353] | 79 | |
---|
[1349] | 80 | ret = roar_vio_socket_init_dstr_def(def, dstr, -1, SOCK_STREAM, def); |
---|
| 81 | |
---|
[1616] | 82 | ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags); |
---|
[1353] | 83 | |
---|
[1349] | 84 | if ( ed != NULL ) |
---|
| 85 | *ed = '/'; |
---|
| 86 | |
---|
[1650] | 87 | ROAR_DBG("roar_vio_proto_init_def(*): dstr='%s'", dstr); |
---|
| 88 | |
---|
[1349] | 89 | return ret; |
---|
[1439] | 90 | #else |
---|
| 91 | return -1; |
---|
| 92 | #endif |
---|
[1349] | 93 | } |
---|
| 94 | |
---|
| 95 | int roar_vio_open_proto (struct roar_vio_calls * calls, struct roar_vio_calls * dst, |
---|
[5254] | 96 | const char * dstr, int proto, struct roar_vio_defaults * odef) { |
---|
[1439] | 97 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[4843] | 98 | struct roar_userpass userpass = {.subtype = -1, .user = NULL, .pass = NULL}; |
---|
[3066] | 99 | struct roar_vio_proto * self; |
---|
[5254] | 100 | const char * host; |
---|
[1349] | 101 | char * tmp; |
---|
| 102 | |
---|
[1616] | 103 | ROAR_DBG("roar_vio_open_proto(calls=%p, dst=%p, dstr='%s', proto=%i, odef=%p) = ?", calls, dst, dstr, proto, odef); |
---|
[1349] | 104 | |
---|
[1353] | 105 | if ( calls == NULL || dst == NULL || odef == NULL ) |
---|
[1349] | 106 | return -1; |
---|
| 107 | |
---|
[1616] | 108 | ROAR_DBG("roar_vio_open_proto(*): odef->o_flags=%i", odef->o_flags); |
---|
[1349] | 109 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
| 110 | |
---|
[3066] | 111 | if ( (self = roar_mm_malloc(sizeof(struct roar_vio_proto))) == NULL ) |
---|
[1349] | 112 | return -1; |
---|
| 113 | |
---|
[3066] | 114 | memset(self, 0, sizeof(struct roar_vio_proto)); |
---|
| 115 | |
---|
| 116 | self->next = dst; |
---|
| 117 | |
---|
| 118 | calls->inst = self; |
---|
| 119 | |
---|
| 120 | calls->read = roar_vio_proto_read; |
---|
| 121 | calls->write = roar_vio_proto_write; |
---|
| 122 | calls->sync = roar_vio_proto_sync; |
---|
| 123 | calls->ctl = roar_vio_proto_ctl; |
---|
| 124 | calls->close = roar_vio_proto_close; |
---|
| 125 | |
---|
[1353] | 126 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
| 127 | |
---|
| 128 | if ( dstr != NULL ) { |
---|
| 129 | dstr += 2; |
---|
| 130 | host = dstr; |
---|
[1349] | 131 | |
---|
[1353] | 132 | if ( (tmp = strstr(dstr, "/")) == NULL ) |
---|
| 133 | return -1; |
---|
| 134 | |
---|
| 135 | *tmp++ = 0; |
---|
| 136 | dstr = tmp; |
---|
[1349] | 137 | |
---|
[1353] | 138 | if ( (tmp = strstr(dstr, "#")) != NULL ) |
---|
| 139 | *tmp = 0; |
---|
| 140 | } else { |
---|
| 141 | ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type); |
---|
| 142 | if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) { |
---|
| 143 | dstr = odef->d.file; |
---|
| 144 | host = "localhost"; |
---|
[1349] | 145 | |
---|
[1353] | 146 | for (; *dstr == '/'; dstr++); |
---|
| 147 | |
---|
| 148 | } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) { |
---|
| 149 | dstr = ""; // index document |
---|
| 150 | host = odef->d.socket.host; |
---|
| 151 | } else { |
---|
| 152 | return -1; |
---|
| 153 | } |
---|
| 154 | } |
---|
[1349] | 155 | |
---|
[4843] | 156 | if ( (tmp = strstr(host, "@")) != NULL ) { |
---|
[5254] | 157 | userpass.user = (char*)host; |
---|
[4843] | 158 | *tmp = 0; |
---|
| 159 | host = tmp + 1; |
---|
| 160 | if ( (tmp = strstr(userpass.user, ":")) != NULL ) { |
---|
| 161 | *tmp = 0; |
---|
| 162 | userpass.pass = tmp + 1; |
---|
| 163 | } |
---|
| 164 | } |
---|
| 165 | |
---|
[1349] | 166 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
[4843] | 167 | ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s', userpass={.user='%s', .pass='%s'}", proto, host, dstr, userpass.user, userpass.pass); |
---|
[1349] | 168 | |
---|
[4828] | 169 | self->proto = proto; |
---|
| 170 | |
---|
[1349] | 171 | switch (proto) { |
---|
| 172 | case ROAR_VIO_PROTO_P_HTTP: |
---|
[3078] | 173 | case ROAR_VIO_PROTO_P_ICY: |
---|
[4843] | 174 | return roar_vio_open_proto_http(calls, dst, host, dstr, userpass.user != NULL ? &userpass : NULL); |
---|
[1349] | 175 | break; |
---|
| 176 | case ROAR_VIO_PROTO_P_GOPHER: |
---|
| 177 | return roar_vio_open_proto_gopher(calls, dst, host, dstr); |
---|
| 178 | break; |
---|
| 179 | } |
---|
| 180 | |
---|
[1650] | 181 | ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol"); |
---|
[1349] | 182 | return -1; |
---|
[1439] | 183 | #else |
---|
| 184 | return -1; |
---|
| 185 | #endif |
---|
[1349] | 186 | } |
---|
| 187 | |
---|
[1439] | 188 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[3065] | 189 | ssize_t roar_vio_proto_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 190 | struct roar_vio_proto * self = vio->inst; |
---|
| 191 | ssize_t ret; |
---|
| 192 | ssize_t have = 0; |
---|
| 193 | size_t len; |
---|
| 194 | |
---|
[4843] | 195 | ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); |
---|
| 196 | |
---|
[3065] | 197 | if ( self->reader.buffer != NULL ) { |
---|
| 198 | len = count; |
---|
| 199 | if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) { |
---|
| 200 | // This is very bad. |
---|
| 201 | return -1; |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | if ( len ) { |
---|
| 205 | have = len; |
---|
| 206 | buf += len; |
---|
| 207 | count -= len; |
---|
| 208 | } |
---|
| 209 | } |
---|
| 210 | |
---|
[4843] | 211 | ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); |
---|
| 212 | |
---|
[3065] | 213 | if ( count == 0 ) |
---|
| 214 | return have; |
---|
| 215 | |
---|
[3067] | 216 | ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); |
---|
| 217 | |
---|
[3065] | 218 | if ( (ret = roar_vio_read(self->next, buf, count)) == -1 ) |
---|
| 219 | return ret; |
---|
| 220 | |
---|
| 221 | return have + ret; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | ssize_t roar_vio_proto_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 225 | struct roar_vio_proto * self = vio->inst; |
---|
| 226 | |
---|
| 227 | return roar_vio_write(self->next, buf, count); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | int roar_vio_proto_sync (struct roar_vio_calls * vio) { |
---|
| 231 | struct roar_vio_proto * self = vio->inst; |
---|
| 232 | |
---|
| 233 | return roar_vio_sync(self->next); |
---|
| 234 | } |
---|
| 235 | |
---|
[5278] | 236 | int roar_vio_proto_ctl (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) { |
---|
[3065] | 237 | struct roar_vio_proto * self = vio->inst; |
---|
| 238 | |
---|
| 239 | if (vio == NULL || cmd == -1) |
---|
| 240 | return -1; |
---|
| 241 | |
---|
| 242 | ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
| 243 | |
---|
| 244 | switch (cmd) { |
---|
[3795] | 245 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 246 | if ( data == NULL ) |
---|
| 247 | return -1; |
---|
| 248 | |
---|
[4828] | 249 | switch (self->proto) { |
---|
| 250 | case ROAR_VIO_PROTO_P_HTTP: |
---|
| 251 | *(char**)data = "http"; |
---|
| 252 | break; |
---|
| 253 | case ROAR_VIO_PROTO_P_GOPHER: |
---|
| 254 | *(char**)data = "gopher"; |
---|
| 255 | break; |
---|
| 256 | case ROAR_VIO_PROTO_P_ICY: |
---|
| 257 | *(char**)data = "icy"; |
---|
| 258 | break; |
---|
| 259 | default: |
---|
| 260 | *(char**)data = "proto"; |
---|
| 261 | break; |
---|
| 262 | } |
---|
[3795] | 263 | return 0; |
---|
| 264 | break; |
---|
[3065] | 265 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 266 | *(struct roar_vio_calls **)data = self->next; |
---|
| 267 | return 0; |
---|
| 268 | break; |
---|
| 269 | case ROAR_VIO_CTL_SET_NEXT: |
---|
| 270 | self->next = *(struct roar_vio_calls **)data; |
---|
| 271 | return 0; |
---|
| 272 | break; |
---|
[4582] | 273 | case ROAR_VIO_CTL_GET_MIMETYPE: |
---|
| 274 | if ( data == NULL ) |
---|
| 275 | return -1; |
---|
| 276 | |
---|
[4827] | 277 | if ( self->content_type == NULL ) |
---|
| 278 | return -1; |
---|
| 279 | |
---|
[4582] | 280 | *(char**)data = self->content_type; |
---|
| 281 | return 0; |
---|
| 282 | break; |
---|
[4839] | 283 | case ROAR_VIO_CTL_GET_FH: |
---|
| 284 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
| 285 | if ( self->reader.buffer == NULL && self->writer.buffer == NULL ) |
---|
| 286 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 287 | return -1; |
---|
| 288 | break; |
---|
| 289 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
| 290 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
| 291 | if ( self->reader.buffer == NULL ) |
---|
| 292 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 293 | return -1; |
---|
| 294 | break; |
---|
| 295 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
| 296 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
| 297 | if ( self->writer.buffer == NULL ) |
---|
| 298 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 299 | return -1; |
---|
| 300 | break; |
---|
[3065] | 301 | } |
---|
| 302 | |
---|
| 303 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | int roar_vio_proto_close (struct roar_vio_calls * vio) { |
---|
| 307 | struct roar_vio_proto * self = vio->inst; |
---|
| 308 | |
---|
| 309 | if ( roar_vio_close(self->next) == -1 ) |
---|
| 310 | return -1; |
---|
| 311 | |
---|
[4582] | 312 | if ( self->content_type != NULL ) |
---|
| 313 | roar_mm_free(self->content_type); |
---|
| 314 | |
---|
[3065] | 315 | roar_mm_free(self); |
---|
| 316 | |
---|
| 317 | return 0; |
---|
| 318 | } |
---|
| 319 | |
---|
[4830] | 320 | static int _parse_header(struct roar_keyval * kv, char ** buf, int * aligned, char * endofheader) { |
---|
[4582] | 321 | char * p = *buf; |
---|
| 322 | char c = 0; |
---|
| 323 | |
---|
| 324 | if ( !(*aligned) ) { |
---|
| 325 | for (; *p != 0 && *p != '\r' && *p != '\n'; p++); |
---|
| 326 | p++; |
---|
| 327 | if ( *p == '\n' ) |
---|
| 328 | p++; |
---|
| 329 | } |
---|
| 330 | |
---|
[4830] | 331 | if ( p >= endofheader ) |
---|
| 332 | return -1; |
---|
| 333 | |
---|
[4843] | 334 | /* |
---|
| 335 | if ( *p == '\r' || *p == '\n' ) |
---|
| 336 | return 0; |
---|
| 337 | */ |
---|
| 338 | |
---|
[4582] | 339 | kv->key = p; |
---|
| 340 | |
---|
| 341 | for (; *p != 0 && *p != '\r' && *p != '\n' && *p != ':'; p++); |
---|
| 342 | |
---|
| 343 | if ( *p == 0 ) |
---|
| 344 | return -1; |
---|
| 345 | |
---|
[4830] | 346 | if ( p >= endofheader ) |
---|
| 347 | return -1; |
---|
| 348 | |
---|
[4582] | 349 | c = *p; |
---|
| 350 | *p = 0; |
---|
| 351 | |
---|
| 352 | if ( c == '\r' && *(p+1) == '\n' ) |
---|
| 353 | p++; |
---|
| 354 | |
---|
| 355 | p++; |
---|
| 356 | |
---|
| 357 | if ( c == '\r' || c == '\n' ) { |
---|
| 358 | if ( *(kv->key) == '\r' || *(kv->key) == '\n' ) |
---|
| 359 | return 0; |
---|
| 360 | // printf("Key-only\n"); |
---|
| 361 | kv->value = kv->key; |
---|
| 362 | kv->key = NULL; |
---|
| 363 | *buf = p; |
---|
| 364 | return 1; |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | for (; *p == ' '; p++); |
---|
| 368 | |
---|
| 369 | if ( *p == 0 ) |
---|
| 370 | return -1; |
---|
| 371 | |
---|
| 372 | kv->value = p; |
---|
| 373 | |
---|
| 374 | for (; *p != 0 && *p != '\r' && *p != '\n'; p++); |
---|
| 375 | |
---|
| 376 | if ( *p == 0 ) |
---|
| 377 | return -1; |
---|
| 378 | |
---|
[4830] | 379 | if ( p >= endofheader ) |
---|
| 380 | return -1; |
---|
| 381 | |
---|
[4582] | 382 | c = *p; |
---|
| 383 | *p = 0; |
---|
| 384 | |
---|
| 385 | if ( c == '\r' && *(p+1) == '\n' ) |
---|
| 386 | p++; |
---|
| 387 | |
---|
| 388 | p++; |
---|
| 389 | |
---|
| 390 | *buf = p; |
---|
| 391 | |
---|
| 392 | if ( c == '\r' || c == '\n' ) { |
---|
| 393 | // printf("aligned\n"); |
---|
| 394 | *aligned = 1; |
---|
| 395 | p++; |
---|
| 396 | } else { |
---|
| 397 | // printf("non-aligned(c=0x%x)\n", (int)c); |
---|
| 398 | *aligned = 0; |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | if ( *(kv->key) != 0 ) |
---|
| 402 | return 1; |
---|
| 403 | |
---|
| 404 | return 0; |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | static void _handle_header (struct roar_vio_proto * self, struct roar_keyval * kv) { |
---|
| 408 | ROAR_DBG("_handle_header(*): Header: key='%s', value='%s'", kv->key, kv->value); |
---|
| 409 | |
---|
| 410 | if ( kv->key == NULL || kv->value == NULL ) |
---|
| 411 | return; |
---|
| 412 | |
---|
| 413 | if ( !strcasecmp(kv->key, "Content-Type") ) { |
---|
| 414 | if ( self->content_type != NULL ) |
---|
| 415 | roar_mm_free(self->content_type); |
---|
| 416 | |
---|
| 417 | self->content_type = roar_mm_strdup(kv->value); |
---|
| 418 | } |
---|
| 419 | } |
---|
[3065] | 420 | |
---|
[4843] | 421 | static inline char * _up2http_auth (struct roar_userpass * up) { |
---|
| 422 | char * inbuf, * outbuf; |
---|
| 423 | size_t inlen, outlen; |
---|
| 424 | ssize_t ret; |
---|
| 425 | //Authorization: Basic dXNlcjpwdw== |
---|
| 426 | |
---|
| 427 | if ( up == NULL ) |
---|
| 428 | return NULL; |
---|
| 429 | |
---|
| 430 | if ( up->subtype != -1 ) |
---|
| 431 | return NULL; |
---|
| 432 | |
---|
| 433 | if ( up->user == NULL || up->pass == NULL ) |
---|
| 434 | return NULL; |
---|
| 435 | |
---|
[5028] | 436 | inlen = roar_mm_strlen(up->user) + roar_mm_strlen(up->pass) + 2; |
---|
[4843] | 437 | inbuf = roar_mm_malloc(inlen); |
---|
| 438 | if ( inbuf == NULL ) |
---|
| 439 | return NULL; |
---|
| 440 | |
---|
| 441 | inbuf[0] = 0; |
---|
[5028] | 442 | roar_mm_strlcat(inbuf, up->user, inlen); |
---|
| 443 | roar_mm_strlcat(inbuf, ":", inlen); |
---|
| 444 | roar_mm_strlcat(inbuf, up->pass, inlen); |
---|
[4843] | 445 | |
---|
| 446 | outlen = ((inlen * 3) / 2) + 3 /* padding... */ + 6 /* 'Basic ' */; |
---|
| 447 | outbuf = roar_mm_malloc(outlen); |
---|
| 448 | if ( outbuf == NULL ) { |
---|
| 449 | roar_mm_free(inbuf); |
---|
| 450 | return NULL; |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | strncpy(outbuf, "Basic ", 7); |
---|
| 454 | |
---|
| 455 | ROAR_DBG("_up2http_auth(up=%p{.subtype=%i, .user='%s', .pass='%s'): inbuf='%s', outbuf='%s'", up, up->subtype, up->user, up->pass, inbuf, outbuf); |
---|
| 456 | ret = roar_base64_encode(NULL, outbuf + 6, outlen - 6, inbuf, inlen - 1, NULL, 1); |
---|
| 457 | ROAR_DBG("_up2http_auth(up=%p{.subtype=%i, .user='%s', .pass='%s'): inbuf='%s', outbuf='%s'", up, up->subtype, up->user, up->pass, inbuf, outbuf); |
---|
| 458 | |
---|
| 459 | roar_mm_free(inbuf); |
---|
| 460 | |
---|
| 461 | if ( ret == -1 ) { |
---|
| 462 | roar_mm_free(outbuf); |
---|
| 463 | return NULL; |
---|
| 464 | } |
---|
| 465 | |
---|
| 466 | return outbuf; |
---|
| 467 | } |
---|
| 468 | |
---|
[5254] | 469 | int roar_vio_open_proto_http (struct roar_vio_calls * calls, struct roar_vio_calls * dst, const char * host, const char * file, struct roar_userpass * up) { |
---|
[4582] | 470 | struct roar_keyval kv; |
---|
[3067] | 471 | struct roar_vio_proto * self; |
---|
| 472 | struct roar_buffer * bufbuf; |
---|
[3788] | 473 | void * vpbuf; |
---|
[4843] | 474 | char * authbuf; |
---|
[3067] | 475 | char * buf; |
---|
| 476 | char * endofheader = NULL; |
---|
[4582] | 477 | char * p; |
---|
[1352] | 478 | char b0[80], b1[80]; |
---|
| 479 | int status; |
---|
| 480 | int len; |
---|
[4582] | 481 | int oeflen = 4; |
---|
| 482 | int aligned = 1; |
---|
[1352] | 483 | |
---|
[1650] | 484 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 485 | |
---|
[1352] | 486 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
| 487 | return -1; |
---|
| 488 | |
---|
[3067] | 489 | self = calls->inst; |
---|
[3066] | 490 | calls->write = NULL; // Disable write as we do not support this |
---|
| 491 | |
---|
[3788] | 492 | if ( roar_buffer_new_data(&bufbuf, 1024, &vpbuf) == -1 ) |
---|
[3067] | 493 | return -1; |
---|
| 494 | |
---|
[3788] | 495 | buf = vpbuf; |
---|
| 496 | |
---|
[1650] | 497 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 498 | |
---|
[1352] | 499 | roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file); |
---|
| 500 | roar_vio_printf(dst, "Host: %s\r\n", host); |
---|
| 501 | roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n"); |
---|
| 502 | roar_vio_printf(dst, "Connection: close\r\n"); |
---|
[4843] | 503 | if ( up != NULL ) { |
---|
| 504 | if ( (authbuf = _up2http_auth(up)) != NULL ) { |
---|
| 505 | roar_vio_printf(dst, "Authorization: %s\r\n", authbuf); |
---|
| 506 | roar_mm_free(authbuf); |
---|
| 507 | } |
---|
| 508 | } |
---|
[1352] | 509 | roar_vio_printf(dst, "\r\n"); |
---|
| 510 | |
---|
[1650] | 511 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 512 | |
---|
[1352] | 513 | roar_vio_sync(dst); |
---|
| 514 | |
---|
[1650] | 515 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 516 | |
---|
| 517 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) { |
---|
| 518 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
[3067] | 519 | roar_buffer_free(bufbuf); |
---|
[1352] | 520 | return -1; |
---|
[1650] | 521 | } |
---|
[1352] | 522 | |
---|
[4830] | 523 | ROAR_DBG("roar_vio_open_proto_http(*): got %i bytes from server.", len); |
---|
| 524 | |
---|
[1352] | 525 | buf[len] = 0; |
---|
| 526 | |
---|
[1650] | 527 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 528 | |
---|
[1352] | 529 | if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) { |
---|
[1650] | 530 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
[3067] | 531 | roar_buffer_free(bufbuf); |
---|
[1352] | 532 | return -1; |
---|
| 533 | } |
---|
| 534 | |
---|
[4830] | 535 | ROAR_DBG("roar_vio_open_proto_http(*): b0='%s'", b0); |
---|
| 536 | |
---|
[1650] | 537 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 538 | |
---|
| 539 | if ( status != 200 ) { |
---|
| 540 | ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status); |
---|
[3067] | 541 | roar_buffer_free(bufbuf); |
---|
[1352] | 542 | return -1; |
---|
[1650] | 543 | } |
---|
[1352] | 544 | |
---|
[1616] | 545 | ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status); |
---|
[1352] | 546 | // ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf); |
---|
| 547 | |
---|
[3067] | 548 | endofheader = strstr(buf, "\r\n\r\n"); |
---|
[4582] | 549 | if ( endofheader == NULL ) { |
---|
| 550 | endofheader = strstr(buf, "\n\n"); |
---|
| 551 | oeflen = 2; |
---|
| 552 | } |
---|
[3067] | 553 | |
---|
| 554 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 555 | |
---|
[4582] | 556 | p = buf; |
---|
[4830] | 557 | while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 ) |
---|
[4582] | 558 | if ( aligned ) |
---|
| 559 | _handle_header(self, &kv); |
---|
| 560 | |
---|
[3067] | 561 | while ( endofheader == NULL ) { |
---|
| 562 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) |
---|
| 563 | return -1; |
---|
| 564 | |
---|
| 565 | buf[len] = 0; |
---|
| 566 | endofheader = strstr(buf, "\r\n\r\n"); |
---|
[4582] | 567 | if ( endofheader == NULL ) { |
---|
| 568 | endofheader = strstr(buf, "\n\n"); |
---|
| 569 | oeflen = 2; |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | /* Doesn't work good. |
---|
[4830] | 573 | while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 ) |
---|
[4582] | 574 | if ( aligned ) |
---|
| 575 | _handle_header(self, &kv); |
---|
| 576 | */ |
---|
| 577 | |
---|
[4830] | 578 | p = buf; |
---|
| 579 | while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 ) |
---|
| 580 | if ( aligned ) |
---|
| 581 | _handle_header(self, &kv); |
---|
| 582 | |
---|
[3067] | 583 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 587 | ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf); |
---|
| 588 | |
---|
[4582] | 589 | if ( (endofheader - buf) == (len - oeflen) ) { |
---|
[3067] | 590 | roar_buffer_free(bufbuf); |
---|
| 591 | bufbuf = NULL; |
---|
| 592 | } |
---|
| 593 | |
---|
| 594 | if ( bufbuf != NULL ) { |
---|
[5254] | 595 | if ( roar_buffer_set_offset(bufbuf, endofheader - buf + oeflen) == -1 || |
---|
| 596 | roar_buffer_set_len(bufbuf, len - (endofheader - buf + oeflen) - 0 /* ??? */) == -1 ) { |
---|
| 597 | // TODO: FIXME: handle this in a better way. |
---|
| 598 | ROAR_ERR("roar_vio_open_proto_http(*): Can not set data area of buffer %p, VERY BAD.", bufbuf); |
---|
| 599 | } |
---|
[3067] | 600 | } |
---|
| 601 | self->reader.buffer = bufbuf; |
---|
| 602 | |
---|
| 603 | /* |
---|
[1352] | 604 | if ( !strcmp((buf+len)-4, "\r\n\r\n") ) |
---|
| 605 | return 0; |
---|
| 606 | |
---|
| 607 | while (*buf != '\r' && *buf != '\n') { |
---|
| 608 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) |
---|
| 609 | return -1; |
---|
| 610 | } |
---|
[3067] | 611 | */ |
---|
[1352] | 612 | |
---|
| 613 | return 0; |
---|
[1349] | 614 | } |
---|
| 615 | |
---|
[5254] | 616 | int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, const char * host, const char * file) { |
---|
[4832] | 617 | struct roar_vio_proto * self; |
---|
| 618 | char type; |
---|
| 619 | const char * mime = NULL; |
---|
| 620 | |
---|
[1349] | 621 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
| 622 | return -1; |
---|
| 623 | |
---|
[4832] | 624 | self = calls->inst; |
---|
[3066] | 625 | calls->write = NULL; // Disable write as we do not support this |
---|
| 626 | |
---|
[1349] | 627 | ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 628 | |
---|
[4832] | 629 | type = file[0]; |
---|
| 630 | |
---|
| 631 | file++; |
---|
| 632 | |
---|
| 633 | ROAR_DBG("roar_vio_open_proto_gopher(*): type='%c'", type); |
---|
[1349] | 634 | |
---|
[4832] | 635 | switch (type) { |
---|
| 636 | case ROAR_GOPHER_TYPE_FILE: |
---|
| 637 | mime = "text/plain"; |
---|
| 638 | break; |
---|
| 639 | case ROAR_GOPHER_TYPE_DIR: |
---|
| 640 | mime = "inode/directory"; |
---|
| 641 | break; |
---|
| 642 | case ROAR_GOPHER_TYPE_BIN: |
---|
| 643 | mime = "application/octet-stream"; |
---|
| 644 | break; |
---|
| 645 | case ROAR_GOPHER_TYPE_GIF: |
---|
| 646 | mime = "image/gif"; |
---|
| 647 | break; |
---|
| 648 | case ROAR_GOPHER_TYPE_HTML: |
---|
| 649 | mime = "text/html"; |
---|
| 650 | break; |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | if ( mime != NULL ) { |
---|
| 654 | self->content_type = roar_mm_strdup(mime); |
---|
| 655 | } |
---|
| 656 | |
---|
| 657 | roar_vio_printf(dst, "%s\r\n", file); |
---|
[1349] | 658 | |
---|
| 659 | roar_vio_sync(dst); // for encryption/compression layers |
---|
| 660 | |
---|
| 661 | return 0; |
---|
| 662 | } |
---|
[1439] | 663 | #endif |
---|
[1349] | 664 | |
---|
[1347] | 665 | //ll |
---|