Skip to content

Commit 0af5160

Browse files
committed
Added Response class
1 parent 935d2ed commit 0af5160

File tree

7 files changed

+160
-4
lines changed

7 files changed

+160
-4
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ add_custom_target(extension COMMAND phpize && ./configure && make && make instal
55
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR})
66

77
set(SOURCE_FILES php_http_message
8-
http_message.c message.c request.c uri.c server_request.c)
8+
http_message.c message.c request.c uri.c server_request.c response.c)
99

1010
execute_process (
1111
COMMAND php-config --include-dir

config.m4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ if test "$PHP_HTTP_MESSAGE" != "no"; then
88
# TODO: Load external depenencies here
99

1010
AC_DEFINE(HAVE_HTTP_MESSAGE, 1, [Whether you have http_message support])
11-
PHP_NEW_EXTENSION(http_message, http_message.c message.c request.c server_request.c uri.c, $ext_shared)
11+
PHP_NEW_EXTENSION(http_message, http_message.c message.c request.c server_request.c response.c uri.c, $ext_shared)
1212
fi
1313

config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ARG_WITH("http_message", "Include http_message support", "no");
55

66
if (PHP_HTTP_MESSAGE != "no") {
77
if (true /* TODO: check external libs */) {
8-
EXTENSION("http_message", "http_message.c message.c request.c server_request.c uri.c");
8+
EXTENSION("http_message", "http_message.c message.c request.c server_request.c response.c uri.c");
99
AC_DEFINE('HAVE_HTTP_MESSAGE', 1 , 'Have http_message support');
1010
PHP_INSTALL_HEADERS("ext/http_message/", "php_http_message.h");
1111
}

http_message.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ PHP_MINIT_FUNCTION(http_message)
5858
PHP_MINIT(http_message_message)(INIT_FUNC_ARGS_PASSTHRU);
5959
PHP_MINIT(http_message_request)(INIT_FUNC_ARGS_PASSTHRU);
6060
PHP_MINIT(http_message_serverrequest)(INIT_FUNC_ARGS_PASSTHRU);
61+
PHP_MINIT(http_message_response)(INIT_FUNC_ARGS_PASSTHRU);
6162
PHP_MINIT(http_message_uri)(INIT_FUNC_ARGS_PASSTHRU);
6263

6364
return SUCCESS;

php_http_message.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static PHP_MINIT_FUNCTION(http_message);
5151
extern PHP_MINIT_FUNCTION(http_message_message);
5252
extern PHP_MINIT_FUNCTION(http_message_request);
5353
extern PHP_MINIT_FUNCTION(http_message_serverrequest);
54+
extern PHP_MINIT_FUNCTION(http_message_response);
5455
extern PHP_MINIT_FUNCTION(http_message_uri);
5556

5657
extern zend_module_entry http_message_module_entry;

response.c

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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

server_request.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
+----------------------------------------------------------------------+
3-
| HTTP Message PHP extension - ServerRequest class |
3+
| HTTP Message PHP extension - ServerRequest class |
44
+----------------------------------------------------------------------+
55
| Copyright (c) 2019 Arnold Daniels |
66
+----------------------------------------------------------------------+

0 commit comments

Comments
 (0)