[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->lseek = roar_vio_proto_lseek; // TODO: this is currently not supported |
---|
| 123 | calls->sync = roar_vio_proto_sync; |
---|
| 124 | calls->ctl = roar_vio_proto_ctl; |
---|
| 125 | calls->close = roar_vio_proto_close; |
---|
| 126 | |
---|
[1353] | 127 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
| 128 | |
---|
| 129 | if ( dstr != NULL ) { |
---|
| 130 | dstr += 2; |
---|
| 131 | host = dstr; |
---|
[1349] | 132 | |
---|
[1353] | 133 | if ( (tmp = strstr(dstr, "/")) == NULL ) |
---|
| 134 | return -1; |
---|
| 135 | |
---|
| 136 | *tmp++ = 0; |
---|
| 137 | dstr = tmp; |
---|
[1349] | 138 | |
---|
[1353] | 139 | if ( (tmp = strstr(dstr, "#")) != NULL ) |
---|
| 140 | *tmp = 0; |
---|
| 141 | } else { |
---|
| 142 | ROAR_DBG("roar_vio_open_proto(*): no dstr!, odef->type=%i", odef->type); |
---|
| 143 | if ( odef->type == ROAR_VIO_DEF_TYPE_FILE ) { |
---|
| 144 | dstr = odef->d.file; |
---|
| 145 | host = "localhost"; |
---|
[1349] | 146 | |
---|
[1353] | 147 | for (; *dstr == '/'; dstr++); |
---|
| 148 | |
---|
| 149 | } else if ( odef->type == ROAR_VIO_DEF_TYPE_SOCKET ) { |
---|
| 150 | dstr = ""; // index document |
---|
| 151 | host = odef->d.socket.host; |
---|
| 152 | } else { |
---|
| 153 | return -1; |
---|
| 154 | } |
---|
| 155 | } |
---|
[1349] | 156 | |
---|
[4843] | 157 | if ( (tmp = strstr(host, "@")) != NULL ) { |
---|
[5254] | 158 | userpass.user = (char*)host; |
---|
[4843] | 159 | *tmp = 0; |
---|
| 160 | host = tmp + 1; |
---|
| 161 | if ( (tmp = strstr(userpass.user, ":")) != NULL ) { |
---|
| 162 | *tmp = 0; |
---|
| 163 | userpass.pass = tmp + 1; |
---|
| 164 | } |
---|
| 165 | } |
---|
| 166 | |
---|
[1349] | 167 | ROAR_DBG("roar_vio_open_proto(*) = ?"); |
---|
[4843] | 168 | 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] | 169 | |
---|
[4828] | 170 | self->proto = proto; |
---|
| 171 | |
---|
[1349] | 172 | switch (proto) { |
---|
| 173 | case ROAR_VIO_PROTO_P_HTTP: |
---|
[3078] | 174 | case ROAR_VIO_PROTO_P_ICY: |
---|
[4843] | 175 | return roar_vio_open_proto_http(calls, dst, host, dstr, userpass.user != NULL ? &userpass : NULL); |
---|
[1349] | 176 | break; |
---|
| 177 | case ROAR_VIO_PROTO_P_GOPHER: |
---|
| 178 | return roar_vio_open_proto_gopher(calls, dst, host, dstr); |
---|
| 179 | break; |
---|
| 180 | } |
---|
| 181 | |
---|
[1650] | 182 | ROAR_DBG("roar_vio_open_proto(*) = -1 // no matching protocol"); |
---|
[1349] | 183 | return -1; |
---|
[1439] | 184 | #else |
---|
| 185 | return -1; |
---|
| 186 | #endif |
---|
[1349] | 187 | } |
---|
| 188 | |
---|
[1439] | 189 | #ifndef ROAR_WITHOUT_VIO_PROTO |
---|
[3065] | 190 | ssize_t roar_vio_proto_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 191 | struct roar_vio_proto * self = vio->inst; |
---|
| 192 | ssize_t ret; |
---|
| 193 | ssize_t have = 0; |
---|
| 194 | size_t len; |
---|
| 195 | |
---|
[4843] | 196 | ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); |
---|
| 197 | |
---|
[3065] | 198 | if ( self->reader.buffer != NULL ) { |
---|
| 199 | len = count; |
---|
| 200 | if ( roar_buffer_shift_out(&(self->reader.buffer), buf, &len) == -1 ) { |
---|
| 201 | // This is very bad. |
---|
| 202 | return -1; |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | if ( len ) { |
---|
| 206 | have = len; |
---|
| 207 | buf += len; |
---|
| 208 | count -= len; |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
[4843] | 212 | ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); |
---|
| 213 | |
---|
[3065] | 214 | if ( count == 0 ) |
---|
| 215 | return have; |
---|
| 216 | |
---|
[3067] | 217 | ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); |
---|
| 218 | |
---|
[3065] | 219 | if ( (ret = roar_vio_read(self->next, buf, count)) == -1 ) |
---|
| 220 | return ret; |
---|
| 221 | |
---|
| 222 | return have + ret; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | ssize_t roar_vio_proto_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 226 | struct roar_vio_proto * self = vio->inst; |
---|
| 227 | |
---|
| 228 | return roar_vio_write(self->next, buf, count); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | // TODO: this is currently not implemented as this is hard to implement with buffers: |
---|
| 232 | off_t roar_vio_proto_lseek (struct roar_vio_calls * vio, off_t offset, int whence); |
---|
| 233 | |
---|
| 234 | int roar_vio_proto_sync (struct roar_vio_calls * vio) { |
---|
| 235 | struct roar_vio_proto * self = vio->inst; |
---|
| 236 | |
---|
| 237 | return roar_vio_sync(self->next); |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | int roar_vio_proto_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 241 | struct roar_vio_proto * self = vio->inst; |
---|
| 242 | |
---|
| 243 | if (vio == NULL || cmd == -1) |
---|
| 244 | return -1; |
---|
| 245 | |
---|
| 246 | ROAR_DBG("roar_vio_proto_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data); |
---|
| 247 | |
---|
| 248 | switch (cmd) { |
---|
[3795] | 249 | case ROAR_VIO_CTL_GET_NAME: |
---|
| 250 | if ( data == NULL ) |
---|
| 251 | return -1; |
---|
| 252 | |
---|
[4828] | 253 | switch (self->proto) { |
---|
| 254 | case ROAR_VIO_PROTO_P_HTTP: |
---|
| 255 | *(char**)data = "http"; |
---|
| 256 | break; |
---|
| 257 | case ROAR_VIO_PROTO_P_GOPHER: |
---|
| 258 | *(char**)data = "gopher"; |
---|
| 259 | break; |
---|
| 260 | case ROAR_VIO_PROTO_P_ICY: |
---|
| 261 | *(char**)data = "icy"; |
---|
| 262 | break; |
---|
| 263 | default: |
---|
| 264 | *(char**)data = "proto"; |
---|
| 265 | break; |
---|
| 266 | } |
---|
[3795] | 267 | return 0; |
---|
| 268 | break; |
---|
[3065] | 269 | case ROAR_VIO_CTL_GET_NEXT: |
---|
| 270 | *(struct roar_vio_calls **)data = self->next; |
---|
| 271 | return 0; |
---|
| 272 | break; |
---|
| 273 | case ROAR_VIO_CTL_SET_NEXT: |
---|
| 274 | self->next = *(struct roar_vio_calls **)data; |
---|
| 275 | return 0; |
---|
| 276 | break; |
---|
[4582] | 277 | case ROAR_VIO_CTL_GET_MIMETYPE: |
---|
| 278 | if ( data == NULL ) |
---|
| 279 | return -1; |
---|
| 280 | |
---|
[4827] | 281 | if ( self->content_type == NULL ) |
---|
| 282 | return -1; |
---|
| 283 | |
---|
[4582] | 284 | *(char**)data = self->content_type; |
---|
| 285 | return 0; |
---|
| 286 | break; |
---|
[4839] | 287 | case ROAR_VIO_CTL_GET_FH: |
---|
| 288 | case ROAR_VIO_CTL_GET_SELECT_FH: |
---|
| 289 | if ( self->reader.buffer == NULL && self->writer.buffer == NULL ) |
---|
| 290 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 291 | return -1; |
---|
| 292 | break; |
---|
| 293 | case ROAR_VIO_CTL_GET_READ_FH: |
---|
| 294 | case ROAR_VIO_CTL_GET_SELECT_READ_FH: |
---|
| 295 | if ( self->reader.buffer == NULL ) |
---|
| 296 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 297 | return -1; |
---|
| 298 | break; |
---|
| 299 | case ROAR_VIO_CTL_GET_WRITE_FH: |
---|
| 300 | case ROAR_VIO_CTL_GET_SELECT_WRITE_FH: |
---|
| 301 | if ( self->writer.buffer == NULL ) |
---|
| 302 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 303 | return -1; |
---|
| 304 | break; |
---|
[3065] | 305 | } |
---|
| 306 | |
---|
| 307 | return roar_vio_ctl(self->next, cmd, data); |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | int roar_vio_proto_close (struct roar_vio_calls * vio) { |
---|
| 311 | struct roar_vio_proto * self = vio->inst; |
---|
| 312 | |
---|
| 313 | if ( roar_vio_close(self->next) == -1 ) |
---|
| 314 | return -1; |
---|
| 315 | |
---|
[4582] | 316 | if ( self->content_type != NULL ) |
---|
| 317 | roar_mm_free(self->content_type); |
---|
| 318 | |
---|
[3065] | 319 | roar_mm_free(self); |
---|
| 320 | |
---|
| 321 | return 0; |
---|
| 322 | } |
---|
| 323 | |
---|
[4830] | 324 | static int _parse_header(struct roar_keyval * kv, char ** buf, int * aligned, char * endofheader) { |
---|
[4582] | 325 | char * p = *buf; |
---|
| 326 | char c = 0; |
---|
| 327 | |
---|
| 328 | if ( !(*aligned) ) { |
---|
| 329 | for (; *p != 0 && *p != '\r' && *p != '\n'; p++); |
---|
| 330 | p++; |
---|
| 331 | if ( *p == '\n' ) |
---|
| 332 | p++; |
---|
| 333 | } |
---|
| 334 | |
---|
[4830] | 335 | if ( p >= endofheader ) |
---|
| 336 | return -1; |
---|
| 337 | |
---|
[4843] | 338 | /* |
---|
| 339 | if ( *p == '\r' || *p == '\n' ) |
---|
| 340 | return 0; |
---|
| 341 | */ |
---|
| 342 | |
---|
[4582] | 343 | kv->key = p; |
---|
| 344 | |
---|
| 345 | for (; *p != 0 && *p != '\r' && *p != '\n' && *p != ':'; p++); |
---|
| 346 | |
---|
| 347 | if ( *p == 0 ) |
---|
| 348 | return -1; |
---|
| 349 | |
---|
[4830] | 350 | if ( p >= endofheader ) |
---|
| 351 | return -1; |
---|
| 352 | |
---|
[4582] | 353 | c = *p; |
---|
| 354 | *p = 0; |
---|
| 355 | |
---|
| 356 | if ( c == '\r' && *(p+1) == '\n' ) |
---|
| 357 | p++; |
---|
| 358 | |
---|
| 359 | p++; |
---|
| 360 | |
---|
| 361 | if ( c == '\r' || c == '\n' ) { |
---|
| 362 | if ( *(kv->key) == '\r' || *(kv->key) == '\n' ) |
---|
| 363 | return 0; |
---|
| 364 | // printf("Key-only\n"); |
---|
| 365 | kv->value = kv->key; |
---|
| 366 | kv->key = NULL; |
---|
| 367 | *buf = p; |
---|
| 368 | return 1; |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | for (; *p == ' '; p++); |
---|
| 372 | |
---|
| 373 | if ( *p == 0 ) |
---|
| 374 | return -1; |
---|
| 375 | |
---|
| 376 | kv->value = p; |
---|
| 377 | |
---|
| 378 | for (; *p != 0 && *p != '\r' && *p != '\n'; p++); |
---|
| 379 | |
---|
| 380 | if ( *p == 0 ) |
---|
| 381 | return -1; |
---|
| 382 | |
---|
[4830] | 383 | if ( p >= endofheader ) |
---|
| 384 | return -1; |
---|
| 385 | |
---|
[4582] | 386 | c = *p; |
---|
| 387 | *p = 0; |
---|
| 388 | |
---|
| 389 | if ( c == '\r' && *(p+1) == '\n' ) |
---|
| 390 | p++; |
---|
| 391 | |
---|
| 392 | p++; |
---|
| 393 | |
---|
| 394 | *buf = p; |
---|
| 395 | |
---|
| 396 | if ( c == '\r' || c == '\n' ) { |
---|
| 397 | // printf("aligned\n"); |
---|
| 398 | *aligned = 1; |
---|
| 399 | p++; |
---|
| 400 | } else { |
---|
| 401 | // printf("non-aligned(c=0x%x)\n", (int)c); |
---|
| 402 | *aligned = 0; |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | if ( *(kv->key) != 0 ) |
---|
| 406 | return 1; |
---|
| 407 | |
---|
| 408 | return 0; |
---|
| 409 | } |
---|
| 410 | |
---|
| 411 | static void _handle_header (struct roar_vio_proto * self, struct roar_keyval * kv) { |
---|
| 412 | ROAR_DBG("_handle_header(*): Header: key='%s', value='%s'", kv->key, kv->value); |
---|
| 413 | |
---|
| 414 | if ( kv->key == NULL || kv->value == NULL ) |
---|
| 415 | return; |
---|
| 416 | |
---|
| 417 | if ( !strcasecmp(kv->key, "Content-Type") ) { |
---|
| 418 | if ( self->content_type != NULL ) |
---|
| 419 | roar_mm_free(self->content_type); |
---|
| 420 | |
---|
| 421 | self->content_type = roar_mm_strdup(kv->value); |
---|
| 422 | } |
---|
| 423 | } |
---|
[3065] | 424 | |
---|
[4843] | 425 | static inline char * _up2http_auth (struct roar_userpass * up) { |
---|
| 426 | char * inbuf, * outbuf; |
---|
| 427 | size_t inlen, outlen; |
---|
| 428 | ssize_t ret; |
---|
| 429 | //Authorization: Basic dXNlcjpwdw== |
---|
| 430 | |
---|
| 431 | if ( up == NULL ) |
---|
| 432 | return NULL; |
---|
| 433 | |
---|
| 434 | if ( up->subtype != -1 ) |
---|
| 435 | return NULL; |
---|
| 436 | |
---|
| 437 | if ( up->user == NULL || up->pass == NULL ) |
---|
| 438 | return NULL; |
---|
| 439 | |
---|
[5028] | 440 | inlen = roar_mm_strlen(up->user) + roar_mm_strlen(up->pass) + 2; |
---|
[4843] | 441 | inbuf = roar_mm_malloc(inlen); |
---|
| 442 | if ( inbuf == NULL ) |
---|
| 443 | return NULL; |
---|
| 444 | |
---|
| 445 | inbuf[0] = 0; |
---|
[5028] | 446 | roar_mm_strlcat(inbuf, up->user, inlen); |
---|
| 447 | roar_mm_strlcat(inbuf, ":", inlen); |
---|
| 448 | roar_mm_strlcat(inbuf, up->pass, inlen); |
---|
[4843] | 449 | |
---|
| 450 | outlen = ((inlen * 3) / 2) + 3 /* padding... */ + 6 /* 'Basic ' */; |
---|
| 451 | outbuf = roar_mm_malloc(outlen); |
---|
| 452 | if ( outbuf == NULL ) { |
---|
| 453 | roar_mm_free(inbuf); |
---|
| 454 | return NULL; |
---|
| 455 | } |
---|
| 456 | |
---|
| 457 | strncpy(outbuf, "Basic ", 7); |
---|
| 458 | |
---|
| 459 | 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); |
---|
| 460 | ret = roar_base64_encode(NULL, outbuf + 6, outlen - 6, inbuf, inlen - 1, NULL, 1); |
---|
| 461 | 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); |
---|
| 462 | |
---|
| 463 | roar_mm_free(inbuf); |
---|
| 464 | |
---|
| 465 | if ( ret == -1 ) { |
---|
| 466 | roar_mm_free(outbuf); |
---|
| 467 | return NULL; |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | return outbuf; |
---|
| 471 | } |
---|
| 472 | |
---|
[5254] | 473 | 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] | 474 | struct roar_keyval kv; |
---|
[3067] | 475 | struct roar_vio_proto * self; |
---|
| 476 | struct roar_buffer * bufbuf; |
---|
[3788] | 477 | void * vpbuf; |
---|
[4843] | 478 | char * authbuf; |
---|
[3067] | 479 | char * buf; |
---|
| 480 | char * endofheader = NULL; |
---|
[4582] | 481 | char * p; |
---|
[1352] | 482 | char b0[80], b1[80]; |
---|
| 483 | int status; |
---|
| 484 | int len; |
---|
[4582] | 485 | int oeflen = 4; |
---|
| 486 | int aligned = 1; |
---|
[1352] | 487 | |
---|
[1650] | 488 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 489 | |
---|
[1352] | 490 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
| 491 | return -1; |
---|
| 492 | |
---|
[3067] | 493 | self = calls->inst; |
---|
[3066] | 494 | calls->write = NULL; // Disable write as we do not support this |
---|
| 495 | |
---|
[3788] | 496 | if ( roar_buffer_new_data(&bufbuf, 1024, &vpbuf) == -1 ) |
---|
[3067] | 497 | return -1; |
---|
| 498 | |
---|
[3788] | 499 | buf = vpbuf; |
---|
| 500 | |
---|
[1650] | 501 | ROAR_DBG("roar_vio_open_proto_http(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 502 | |
---|
[1352] | 503 | roar_vio_printf(dst, "GET /%s HTTP/1.1\r\n", file); |
---|
| 504 | roar_vio_printf(dst, "Host: %s\r\n", host); |
---|
| 505 | roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n"); |
---|
| 506 | roar_vio_printf(dst, "Connection: close\r\n"); |
---|
[4843] | 507 | if ( up != NULL ) { |
---|
| 508 | if ( (authbuf = _up2http_auth(up)) != NULL ) { |
---|
| 509 | roar_vio_printf(dst, "Authorization: %s\r\n", authbuf); |
---|
| 510 | roar_mm_free(authbuf); |
---|
| 511 | } |
---|
| 512 | } |
---|
[1352] | 513 | roar_vio_printf(dst, "\r\n"); |
---|
| 514 | |
---|
[1650] | 515 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 516 | |
---|
[1352] | 517 | roar_vio_sync(dst); |
---|
| 518 | |
---|
[1650] | 519 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 520 | |
---|
| 521 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) { |
---|
| 522 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
[3067] | 523 | roar_buffer_free(bufbuf); |
---|
[1352] | 524 | return -1; |
---|
[1650] | 525 | } |
---|
[1352] | 526 | |
---|
[4830] | 527 | ROAR_DBG("roar_vio_open_proto_http(*): got %i bytes from server.", len); |
---|
| 528 | |
---|
[1352] | 529 | buf[len] = 0; |
---|
| 530 | |
---|
[1650] | 531 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 532 | |
---|
[1352] | 533 | if ( sscanf(buf, "%79s %i %79s\n", b0, &status, b1) != 3 ) { |
---|
[1650] | 534 | ROAR_DBG("roar_vio_open_proto_http(*) = -1"); |
---|
[3067] | 535 | roar_buffer_free(bufbuf); |
---|
[1352] | 536 | return -1; |
---|
| 537 | } |
---|
| 538 | |
---|
[4830] | 539 | ROAR_DBG("roar_vio_open_proto_http(*): b0='%s'", b0); |
---|
| 540 | |
---|
[1650] | 541 | ROAR_DBG("roar_vio_open_proto_http(*) = ?"); |
---|
| 542 | |
---|
| 543 | if ( status != 200 ) { |
---|
| 544 | ROAR_DBG("roar_vio_open_proto_http(*) = -1 // status=%i", status); |
---|
[3067] | 545 | roar_buffer_free(bufbuf); |
---|
[1352] | 546 | return -1; |
---|
[1650] | 547 | } |
---|
[1352] | 548 | |
---|
[1616] | 549 | ROAR_DBG("roar_vio_open_proto_http(*): status=%i", status); |
---|
[1352] | 550 | // ROAR_WARN("roar_vio_open_proto_http(*): buf='%s'", buf); |
---|
| 551 | |
---|
[3067] | 552 | endofheader = strstr(buf, "\r\n\r\n"); |
---|
[4582] | 553 | if ( endofheader == NULL ) { |
---|
| 554 | endofheader = strstr(buf, "\n\n"); |
---|
| 555 | oeflen = 2; |
---|
| 556 | } |
---|
[3067] | 557 | |
---|
| 558 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 559 | |
---|
[4582] | 560 | p = buf; |
---|
[4830] | 561 | while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 ) |
---|
[4582] | 562 | if ( aligned ) |
---|
| 563 | _handle_header(self, &kv); |
---|
| 564 | |
---|
[3067] | 565 | while ( endofheader == NULL ) { |
---|
| 566 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) |
---|
| 567 | return -1; |
---|
| 568 | |
---|
| 569 | buf[len] = 0; |
---|
| 570 | endofheader = strstr(buf, "\r\n\r\n"); |
---|
[4582] | 571 | if ( endofheader == NULL ) { |
---|
| 572 | endofheader = strstr(buf, "\n\n"); |
---|
| 573 | oeflen = 2; |
---|
| 574 | } |
---|
| 575 | |
---|
| 576 | /* Doesn't work good. |
---|
[4830] | 577 | while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 ) |
---|
[4582] | 578 | if ( aligned ) |
---|
| 579 | _handle_header(self, &kv); |
---|
| 580 | */ |
---|
| 581 | |
---|
[4830] | 582 | p = buf; |
---|
| 583 | while ( _parse_header(&kv, &p, &aligned, endofheader) > 0 ) |
---|
| 584 | if ( aligned ) |
---|
| 585 | _handle_header(self, &kv); |
---|
| 586 | |
---|
[3067] | 587 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 588 | } |
---|
| 589 | |
---|
| 590 | ROAR_DBG("roar_vio_open_proto_http(*): endofheader=%p\n", endofheader); |
---|
| 591 | ROAR_DBG("roar_vio_open_proto_http(*): buf=%p\n", buf); |
---|
| 592 | |
---|
[4582] | 593 | if ( (endofheader - buf) == (len - oeflen) ) { |
---|
[3067] | 594 | roar_buffer_free(bufbuf); |
---|
| 595 | bufbuf = NULL; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | if ( bufbuf != NULL ) { |
---|
[5254] | 599 | if ( roar_buffer_set_offset(bufbuf, endofheader - buf + oeflen) == -1 || |
---|
| 600 | roar_buffer_set_len(bufbuf, len - (endofheader - buf + oeflen) - 0 /* ??? */) == -1 ) { |
---|
| 601 | // TODO: FIXME: handle this in a better way. |
---|
| 602 | ROAR_ERR("roar_vio_open_proto_http(*): Can not set data area of buffer %p, VERY BAD.", bufbuf); |
---|
| 603 | } |
---|
[3067] | 604 | } |
---|
| 605 | self->reader.buffer = bufbuf; |
---|
| 606 | |
---|
| 607 | /* |
---|
[1352] | 608 | if ( !strcmp((buf+len)-4, "\r\n\r\n") ) |
---|
| 609 | return 0; |
---|
| 610 | |
---|
| 611 | while (*buf != '\r' && *buf != '\n') { |
---|
| 612 | if ( (len = roar_vio_read(dst, buf, 1023)) < 1 ) |
---|
| 613 | return -1; |
---|
| 614 | } |
---|
[3067] | 615 | */ |
---|
[1352] | 616 | |
---|
| 617 | return 0; |
---|
[1349] | 618 | } |
---|
| 619 | |
---|
[5254] | 620 | int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, const char * host, const char * file) { |
---|
[4832] | 621 | struct roar_vio_proto * self; |
---|
| 622 | char type; |
---|
| 623 | const char * mime = NULL; |
---|
| 624 | |
---|
[1349] | 625 | if ( calls == NULL || dst == NULL || host == NULL || file == NULL ) |
---|
| 626 | return -1; |
---|
| 627 | |
---|
[4832] | 628 | self = calls->inst; |
---|
[3066] | 629 | calls->write = NULL; // Disable write as we do not support this |
---|
| 630 | |
---|
[1349] | 631 | ROAR_DBG("roar_vio_open_proto_gopher(calls=%p, dst=%p, host='%s', file='%s') = ?", calls, dst, host, file); |
---|
| 632 | |
---|
[4832] | 633 | type = file[0]; |
---|
| 634 | |
---|
| 635 | file++; |
---|
| 636 | |
---|
| 637 | ROAR_DBG("roar_vio_open_proto_gopher(*): type='%c'", type); |
---|
[1349] | 638 | |
---|
[4832] | 639 | switch (type) { |
---|
| 640 | case ROAR_GOPHER_TYPE_FILE: |
---|
| 641 | mime = "text/plain"; |
---|
| 642 | break; |
---|
| 643 | case ROAR_GOPHER_TYPE_DIR: |
---|
| 644 | mime = "inode/directory"; |
---|
| 645 | break; |
---|
| 646 | case ROAR_GOPHER_TYPE_BIN: |
---|
| 647 | mime = "application/octet-stream"; |
---|
| 648 | break; |
---|
| 649 | case ROAR_GOPHER_TYPE_GIF: |
---|
| 650 | mime = "image/gif"; |
---|
| 651 | break; |
---|
| 652 | case ROAR_GOPHER_TYPE_HTML: |
---|
| 653 | mime = "text/html"; |
---|
| 654 | break; |
---|
| 655 | } |
---|
| 656 | |
---|
| 657 | if ( mime != NULL ) { |
---|
| 658 | self->content_type = roar_mm_strdup(mime); |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | roar_vio_printf(dst, "%s\r\n", file); |
---|
[1349] | 662 | |
---|
| 663 | roar_vio_sync(dst); // for encryption/compression layers |
---|
| 664 | |
---|
| 665 | return 0; |
---|
| 666 | } |
---|
[1439] | 667 | #endif |
---|
[1349] | 668 | |
---|
[1347] | 669 | //ll |
---|