[3377] | 1 | //httpd.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010 |
---|
| 5 | * |
---|
| 6 | * This file is part of libroareio 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 | * libroardsp 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 |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #include "libroareio.h" |
---|
| 26 | |
---|
[3381] | 27 | struct 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 | |
---|
| 45 | int 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: |
---|
| 58 | static 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 | |
---|
| 91 | int 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 | |
---|
[3377] | 159 | //ll |
---|