Line data Source code
1 : /* 2 : * lwan - web server 3 : * Copyright (c) 2017 L. A. F. Pereira <l@tia.mat.br> 4 : * 5 : * This program is free software; you can redistribute it and/or 6 : * modify it under the terms of the GNU General Public License 7 : * as published by the Free Software Foundation; either version 2 8 : * of the License, or any later version. 9 : * 10 : * This program is distributed in the hope that it will be useful, 11 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 : * GNU General Public License for more details. 14 : * 15 : * You should have received a copy of the GNU General Public License 16 : * along with this program; if not, write to the Free Software 17 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 18 : * USA. 19 : */ 20 : 21 : #include <stdlib.h> 22 : #include <string.h> 23 : 24 : #include "lwan-private.h" 25 : #include "lwan-mod-response.h" 26 : 27 : static enum lwan_http_status 28 1 : response_handle_request(struct lwan_request *request __attribute__((unused)), 29 : struct lwan_response *response __attribute__((unused)), 30 : void *instance) 31 : { 32 1 : return (enum lwan_http_status)instance; 33 : } 34 : 35 87 : static void *response_create(const char *prefix __attribute__((unused)), 36 : void *instance) 37 : { 38 87 : struct lwan_response_settings *settings = instance; 39 : 40 : const char *valid_code = 41 87 : lwan_http_status_as_string_with_code(settings->code); 42 87 : if (!strncmp(valid_code, "999 ", 4)) { 43 0 : lwan_status_error("Code %d isn't a known HTTP status code", 44 : settings->code); 45 0 : return NULL; 46 : } 47 : 48 87 : return (void *)settings->code; 49 : } 50 : 51 87 : static void *response_create_from_hash(const char *prefix, 52 : const struct hash *hash) 53 : { 54 87 : const char *code = hash_find(hash, "code"); 55 : 56 87 : if (!code) { 57 0 : lwan_status_error("`code` not supplied"); 58 0 : return NULL; 59 : } 60 : 61 87 : int code_as_int = parse_int(code, -1); 62 87 : if (code_as_int < 0) { 63 0 : lwan_status_error("Couldn't parse `code` as an integer"); 64 0 : return NULL; 65 : } 66 : 67 87 : struct lwan_response_settings settings = { 68 87 : .code = (enum lwan_http_status)code_as_int, 69 : }; 70 87 : return response_create(prefix, &settings); 71 : } 72 : 73 : static const struct lwan_module module = { 74 : .create = response_create, 75 : .create_from_hash = response_create_from_hash, 76 : .handle_request = response_handle_request, 77 : }; 78 : 79 : LWAN_REGISTER_MODULE(response, &module);