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