|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | HTTP Message PHP extension - Response class | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 2019 Arnold Daniels | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | Permission is hereby granted, free of charge, to any person | |
| 8 | + | obtaining a copy of this software and associated documentation files | |
| 9 | + | (the "Software"), to deal in the Software without restriction, | |
| 10 | + | including without limitation the rights to use, copy, modify, merge, | |
| 11 | + | publish, distribute, sublicense, and/or sell copies of the Software, | |
| 12 | + | and to permit persons to whom the Software is furnished to do so, | |
| 13 | + | subject to the following conditions: | |
| 14 | + | | |
| 15 | + | The above copyright notice and this permission notice shall be | |
| 16 | + | included in all copies or substantial portions of the Software. | |
| 17 | + | | |
| 18 | + | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
| 19 | + | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
| 20 | + | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
| 21 | + | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
| 22 | + | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
| 23 | + | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
| 24 | + | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 25 | + | SOFTWARE. | |
| 26 | + +----------------------------------------------------------------------+ |
| 27 | + | Author: Arnold Daniels <arnold@jasny.net> | |
| 28 | + +----------------------------------------------------------------------+ |
| 29 | +*/ |
| 30 | + |
| 31 | +#ifdef HAVE_CONFIG_H |
| 32 | +# include "config.h" |
| 33 | +#endif |
| 34 | + |
| 35 | +#include "php.h" |
| 36 | +#include "php_ini.h" |
| 37 | +#include "php_http_message.h" |
| 38 | +#include "macros.h" |
| 39 | +#include "zend_exceptions.h" |
| 40 | +#include "zend_interfaces.h" |
| 41 | +#include "http_status_codes.h" |
| 42 | +#include "ext/standard/info.h" |
| 43 | +#include "ext/psr/psr_http_message.h" |
| 44 | + |
| 45 | +#if HAVE_HTTP_MESSAGE |
| 46 | + |
| 47 | +zend_class_entry *HttpMessage_Response_ce; |
| 48 | + |
| 49 | + |
| 50 | +/* Helper function to get reason phrase from status code */ |
| 51 | + |
| 52 | +static int status_comp(const void *a, const void *b) |
| 53 | +{ |
| 54 | + const http_response_status_code_pair *pa = (const http_response_status_code_pair *) a; |
| 55 | + const http_response_status_code_pair *pb = (const http_response_status_code_pair *) b; |
| 56 | + |
| 57 | + if (pa->code < pb->code) { |
| 58 | + return -1; |
| 59 | + } else if (pa->code > pb->code) { |
| 60 | + return 1; |
| 61 | + } |
| 62 | + |
| 63 | + return 0; |
| 64 | +} |
| 65 | + |
| 66 | +static const char *get_status_string(int code) |
| 67 | +{ |
| 68 | + http_response_status_code_pair needle = {code, NULL}, *result = NULL; |
| 69 | + |
| 70 | + result = bsearch(&needle, http_status_map, http_status_map_len, sizeof(needle), status_comp); |
| 71 | + |
| 72 | + return result ? result->str : ""; |
| 73 | +} |
| 74 | + |
| 75 | + |
| 76 | +/* __construct */ |
| 77 | + |
| 78 | +ZEND_BEGIN_ARG_INFO_EX(arginfo_Response_construct, 0, 0, 0) |
| 79 | +ZEND_END_ARG_INFO() |
| 80 | + |
| 81 | +/* status */ |
| 82 | + |
| 83 | +PHP_METHOD(Response, getStatusCode) |
| 84 | +{ |
| 85 | + zval rv, *value; |
| 86 | + |
| 87 | + value = zend_read_property(HttpMessage_Response_ce, getThis(), ZEND_STRL("statusCode"), 0, &rv); |
| 88 | + |
| 89 | + RETURN_ZVAL(value, 1, 0); |
| 90 | +} |
| 91 | + |
| 92 | +PHP_METHOD(Response, getReasonPhrase) |
| 93 | +{ |
| 94 | + zval rv, *value; |
| 95 | + |
| 96 | + value = zend_read_property(HttpMessage_Response_ce, getThis(), ZEND_STRL("reasonPhrase"), 0, &rv); |
| 97 | + |
| 98 | + RETURN_ZVAL(value, 1, 0); |
| 99 | +} |
| 100 | + |
| 101 | +PHP_METHOD(Response, withStatus) |
| 102 | +{ |
| 103 | + zend_long code; |
| 104 | + char *phrase; |
| 105 | + size_t phrase_len; |
| 106 | + const char *suggested_phrase; |
| 107 | + |
| 108 | + ZEND_PARSE_PARAMETERS_START_EX(ZEND_PARSE_PARAMS_THROW, 1, 1) |
| 109 | + Z_PARAM_LONG(code) |
| 110 | + Z_PARAM_STRING(phrase, phrase_len) |
| 111 | + ZEND_PARSE_PARAMETERS_END_EX(); |
| 112 | + |
| 113 | + ZVAL_OBJ(return_value, zend_objects_clone_obj(getThis())); |
| 114 | + |
| 115 | + zend_update_property_long(HttpMessage_Response_ce, return_value, ZEND_STRL("statusCode"), code); |
| 116 | + |
| 117 | + if (phrase_len > 0) { |
| 118 | + zend_update_property_stringl( |
| 119 | + HttpMessage_Response_ce, return_value, ZEND_STRL("reasonPhrase"), phrase, phrase_len |
| 120 | + ); |
| 121 | + } else { |
| 122 | + suggested_phrase = get_status_string((int)code); |
| 123 | + zend_update_property_stringl( |
| 124 | + HttpMessage_Response_ce, return_value, ZEND_STRL("reasonPhrase"), ZEND_STRL(suggested_phrase) |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +/* Define HttpMessage\Response class */ |
| 131 | + |
| 132 | +static const zend_function_entry response_functions[] = { |
| 133 | + HTTP_MESSAGE_ME(Response, getStatusCode) |
| 134 | + HTTP_MESSAGE_ME(Response, getReasonPhrase) |
| 135 | + HTTP_MESSAGE_ME(Response, withStatus) |
| 136 | + PHP_FE_END |
| 137 | +}; |
| 138 | + |
| 139 | +PHP_MINIT_FUNCTION(http_message_response) |
| 140 | +{ |
| 141 | + zend_class_entry ce; |
| 142 | + INIT_NS_CLASS_ENTRY(ce, "HttpMessage", "Response", response_functions); |
| 143 | + |
| 144 | + HttpMessage_Response_ce = zend_register_internal_class_ex(&ce, HttpMessage_Message_ce); |
| 145 | + zend_class_implements(HttpMessage_Response_ce, 1, PsrHttpMessageResponseInterface_ce_ptr); |
| 146 | + |
| 147 | + /* Properties */ |
| 148 | + zend_declare_property_long(HttpMessage_Response_ce, ZEND_STRL("statusCode"), 0, ZEND_ACC_PROTECTED); |
| 149 | + zend_declare_property_string(HttpMessage_Response_ce, ZEND_STRL("reasonPhrase"), "", ZEND_ACC_PROTECTED); |
| 150 | + |
| 151 | + return SUCCESS; |
| 152 | +} |
| 153 | + |
| 154 | +#endif |
0 commit comments