Changeset 4843:05ca8907fa35 in roaraudio


Ignore:
Timestamp:
04/05/11 00:18:34 (13 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

Added support for HTTP Basic Auth

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ChangeLog

    r4824 r4843  
    1010        * Added Record Bridge to roard. 
    1111        * Added VIOs and DSTR elements: null:, zero:, nrandom: 
     12        * Added support for HTTP Basic Auth 
    1213 
    1314v. 0.4beta4 - Sun Mar 20 2011 12:15 CET 
  • include/libroar/vio_proto.h

    r4828 r4843  
    7373int     roar_vio_proto_close   (struct roar_vio_calls * vio); 
    7474 
    75 int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file); 
     75int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file, struct roar_userpass * up); 
    7676int roar_vio_open_proto_gopher (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file); 
    7777#endif 
  • libroar/vio_proto.c

    r4839 r4843  
    4545 int                        ret; 
    4646 char                     * ed; 
     47 char                     * tmp; 
    4748 
    4849 if ( def == NULL ) 
     
    7273  *ed = 0; 
    7374 
     75 if ( (tmp = strstr(dstr, "@")) != NULL ) 
     76  dstr = tmp + 1; 
     77 
    7478 ROAR_DBG("roar_vio_proto_init_def(*): def->o_flags=%i", def->o_flags); 
    7579 
     
    9296                              char * dstr, int proto, struct roar_vio_defaults * odef) { 
    9397#ifndef ROAR_WITHOUT_VIO_PROTO 
     98 struct roar_userpass userpass = {.subtype = -1, .user = NULL, .pass = NULL}; 
    9499 struct roar_vio_proto * self; 
    95100 char * host; 
     
    151156 } 
    152157 
     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 
    153168 ROAR_DBG("roar_vio_open_proto(*) = ?"); 
    154  ROAR_DBG("roar_vio_open_proto(*): proto=%i, host='%s', file='%s'", proto, host, dstr); 
     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); 
    155170 
    156171 self->proto = proto; 
     
    159174  case ROAR_VIO_PROTO_P_HTTP: 
    160175  case ROAR_VIO_PROTO_P_ICY: 
    161     return roar_vio_open_proto_http(calls, dst, host, dstr); 
     176    return roar_vio_open_proto_http(calls, dst, host, dstr, userpass.user != NULL ? &userpass : NULL); 
    162177   break; 
    163178  case ROAR_VIO_PROTO_P_GOPHER: 
     
    180195 size_t  len; 
    181196 
     197 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); 
     198 
    182199 if ( self->reader.buffer != NULL ) { 
    183200  len = count; 
     
    193210  } 
    194211 } 
     212 
     213 ROAR_DBG("roar_vio_proto_read(*): have=%lli, count=%lli", (long long int)have, (long long int)count); 
    195214 
    196215 if ( count == 0 ) 
     
    327346  return -1; 
    328347 
     348/* 
     349 if ( *p == '\r' || *p == '\n' ) 
     350  return 0; 
     351 */ 
     352 
    329353 kv->key = p; 
    330354 
     
    409433} 
    410434 
    411 int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file) { 
     435static 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 
     450 inlen = strlen(up->user) + strlen(up->pass) + 2; 
     451 inbuf = roar_mm_malloc(inlen); 
     452 if ( inbuf == NULL ) 
     453  return NULL; 
     454 
     455 inbuf[0] = 0; 
     456 strcat(inbuf, up->user); 
     457 strcat(inbuf, ":"); 
     458 strcat(inbuf, up->pass); 
     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 
     483int roar_vio_open_proto_http   (struct roar_vio_calls * calls, struct roar_vio_calls * dst, char * host, char * file, struct roar_userpass * up) { 
    412484 struct roar_keyval kv; 
    413485 struct roar_vio_proto * self; 
    414486 struct roar_buffer * bufbuf; 
    415487 void * vpbuf; 
     488 char * authbuf; 
    416489 char * buf; 
    417490 char * endofheader = NULL; 
     
    442515 roar_vio_printf(dst, "User-Agent: roar_vio_open_proto_http() $Revision$\r\n"); 
    443516 roar_vio_printf(dst, "Connection: close\r\n"); 
     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 } 
    444523 roar_vio_printf(dst, "\r\n"); 
    445524 
     
    529608 if ( bufbuf != NULL ) { 
    530609  roar_buffer_set_offset(bufbuf, endofheader - buf + oeflen); 
    531   roar_buffer_set_len(bufbuf,    len - (endofheader - buf + oeflen) - 1); 
     610  roar_buffer_set_len(bufbuf,    len - (endofheader - buf + oeflen) - 0 /* ??? */); 
    532611 } 
    533612 self->reader.buffer = bufbuf; 
Note: See TracChangeset for help on using the changeset viewer.