Changeset 3381:b883692000fa in roaraudio for libroareio


Ignore:
Timestamp:
02/11/10 11:55:10 (14 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

a lot code, does not work at all, but is on the best way

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroareio/httpd.c

    r3377 r3381  
    2525#include "libroareio.h" 
    2626 
     27struct roar_httpd * roar_http_new(struct roar_vio_calls * client, int (*cb_eoh)(struct roar_httpd * httpd)) { 
     28 struct roar_httpd * httpd; 
     29 
     30 if ( client == NULL || cb_eoh == NULL ) 
     31  return NULL; 
     32 
     33 if ( (httpd = roar_mm_malloc(sizeof(struct roar_httpd))) == NULL ) 
     34  return NULL; 
     35 
     36 memset(httpd, 0, sizeof(struct roar_httpd)); 
     37 
     38 httpd->state  = ROAR_HTTPD_STATE_REQ; 
     39 httpd->client = client; 
     40 httpd->cb_eoh = cb_eoh; 
     41 
     42 return NULL; 
     43} 
     44 
     45int                 roar_http_free(struct roar_httpd * httpd) { 
     46 if ( httpd->response.file != NULL ) 
     47  roar_vio_close(httpd->response.file); 
     48 
     49 if ( httpd->client != NULL ) 
     50  roar_vio_close(httpd->client); 
     51 
     52 roar_mm_free(httpd); 
     53 
     54 return 0; 
     55} 
     56 
     57// TODO: update this function to use iobuffer: 
     58static int roar_http_eoh (struct roar_httpd * httpd) { 
     59 struct roar_httpd_request  * req  = &(httpd->request); 
     60 struct roar_httpd_response * resp = &(httpd->response); 
     61 int i; 
     62 
     63 // TODO: parse HTTP here 
     64 
     65 resp->version = req->version; 
     66 resp->status  = -1; 
     67 httpd->cb_eoh(httpd); 
     68 
     69 if ( resp->status == -1 ) { 
     70  if ( resp->file == NULL ) { 
     71   resp->status = ROAR_HTTPD_STATUS_NOT_FOUND; 
     72  } else { 
     73   resp->status = ROAR_HTTPD_STATUS_OK; 
     74  } 
     75 } 
     76 
     77 roar_vio_printf(httpd->client, "HTTP/%i.%i %i State %i\r\n", _ROAR_EIO_HTTPD_VERSION_MAJOR(resp->version), 
     78                                                              _ROAR_EIO_HTTPD_VERSION_MINOR(resp->version), 
     79                                                              resp->status, resp->status); 
     80 
     81 if ( resp->header != NULL ) { 
     82  for (i = 0; resp->header[i].key != NULL; i++) { 
     83   roar_vio_printf(httpd->client, "%s: %s\r\n", resp->header[i].key, resp->header[i].value); 
     84  } 
     85  roar_mm_free(resp->header); 
     86 } 
     87 
     88 return 0; 
     89} 
     90 
     91int                 roar_http_update(struct roar_httpd * httpd) { 
     92 void *  data; 
     93 size_t  len; 
     94 ssize_t ret; 
     95 
     96 switch (httpd->state) { 
     97  case ROAR_HTTPD_STATE_REQ: 
     98   break; 
     99  case ROAR_HTTPD_STATE_EOH: 
     100    return roar_http_eoh(httpd); 
     101   break; 
     102  case ROAR_HTTPD_STATE_RESP: 
     103   if ( httpd->iobuffer != NULL ) { 
     104    if ( roar_buffer_get_len(httpd->iobuffer, &len) == -1 ) 
     105     return -1; 
     106   } else { 
     107    len = 0; 
     108   } 
     109 
     110   if ( len != 0 ) { 
     111    if ( roar_buffer_get_data(httpd->iobuffer, &data) == -1 ) 
     112     return -1; 
     113 
     114    if ( (ret = roar_vio_write(httpd->client, data, len)) == -1 ) 
     115     return -1; 
     116 
     117    if ( roar_buffer_set_offset(httpd->iobuffer, ret) == -1 ) 
     118     return -1; // bad error 
     119 
     120    return 0; 
     121   } 
     122 
     123   if ( httpd->iobuffer != NULL ) { 
     124    len = 2048; 
     125    if ( roar_buffer_set_len(httpd->iobuffer, len) == -1 ) 
     126     return -1; 
     127   } else { 
     128    if ( roar_buffer_new(&(httpd->iobuffer), len) == -1 ) 
     129     return -1; 
     130   } 
     131 
     132   if ( roar_buffer_get_data(httpd->iobuffer, &data) == -1 ) 
     133    return -1; 
     134 
     135   if ( (ret = roar_vio_read(httpd->response.file, data, len)) == -1 ) { 
     136    if ( roar_buffer_set_len(httpd->iobuffer, 0) == -1 ) 
     137     return -1; // bad error 
     138   } 
     139 
     140   len = ret; 
     141 
     142   if ( roar_buffer_set_len(httpd->iobuffer, len) == -1 ) 
     143    return -1; 
     144 
     145   if ( (ret = roar_vio_write(httpd->client, data, len)) == -1 ) 
     146    return -1; 
     147 
     148   if ( roar_buffer_set_offset(httpd->iobuffer, ret) == -1 ) 
     149    return -1; // bad error 
     150 
     151   return 0; 
     152   break; 
     153 } 
     154 
     155 return -1; 
     156} 
     157 
     158 
    27159//ll 
Note: See TracChangeset for help on using the changeset viewer.