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