-
Notifications
You must be signed in to change notification settings - Fork 1
/
myhttp.c
195 lines (164 loc) · 5.32 KB
/
myhttp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <Python.h>
#include "include/http/http.h"
#include <stdbool.h>
#include <string.h>
#define NO_CONF 0
#define EXCLUDE_BODY_CONF 1
#define LOCALHOST "localhost"
#define ENCODING_UTF8 "UTF-8"
#define IF_THEN_NULL(cond) \
if(cond) return NULL;
#define IF_NULL_THEN_NULL(ptr) \
IF_THEN_NULL(ptr == NULL)
static PyObject * py_http_request(PyObject *, PyObject *, int, int);
static PyObject * py_get_request(PyObject *self, PyObject *args) {
return py_http_request(self, args, HTTP_METHOD_GET, NO_CONF);
}
static PyObject * py_post_request(PyObject *self, PyObject *args) {
return py_http_request(self, args, HTTP_METHOD_POST, NO_CONF);
}
static PyObject * py_head_request(PyObject *self, PyObject *args) {
return py_http_request(self, args, HTTP_METHOD_HEAD, EXCLUDE_BODY_CONF);
}
/*
* < Private functions >
*/
static bool check_error_init(int err_code) {
switch(err_code) {
case ERROR_INIT_METHOD:
PyErr_SetString(PyExc_ValueError, "Invalid Request Method");
break;
case ERROR_INIT_HOST:
PyErr_SetString(PyExc_ValueError, "Invalid Request Host");
break;
case ERROR_INIT_URL:
PyErr_SetString(PyExc_ValueError, "Invalid Request URL");
break;
}
return err_code != ERROR_NO;
}
static bool check_error_send(int err_code) {
switch(err_code) {
case ERROR_SEND_SOCKET_CREATE:
PyErr_SetString(PyExc_ConnectionError, "Socket creation failed");
break;
case ERROR_SEND_NO_HOST:
PyErr_SetString(PyExc_ConnectionError, "Host not found");
break;
case ERROR_SEND_SOCKET_CONNECT:
PyErr_SetString(PyExc_ConnectionError, "Socket connection failed");
break;
case ERROR_SEND_WRITE_REQUEST:
PyErr_SetString(PyExc_ConnectionError, "Can't send bytes");
break;
case ERROR_SEND_READ_RESPONSE:
PyErr_SetString(PyExc_ConnectionError, "Can't receive bytes");
break;
}
return err_code != ERROR_NO;
}
static bool is_localhost(char * value, int * port) {
const char * localhost_str = LOCALHOST;
int index = 0;
for(; localhost_str[index] != '\0'; index++) {
if(localhost_str[index] != value[index]) {
return false;
}
}
if(value[index] == ':' && isdigit(value[index + 1])) {
*port = atoi(value + index + 1);
} else {
*port = -1;
}
return true;
}
/*
* </ Private functions >
*/
static PyObject * py_http_request(
PyObject *self,
PyObject *args,
int method_code,
int conf
) {
char * host, * url, * body = NULL;
PyObject * dict = NULL;
int parsed_result = IF_ELSE(
conf == EXCLUDE_BODY_CONF,
PyArg_ParseTuple(args, "ss|O", &host, &url, &dict),
PyArg_ParseTuple(args, "ssz|O", &host, &url, &body, &dict)
);
IF_THEN_NULL(!parsed_result);
int port = -1;
bool is_local = is_localhost(host, &port);
if(port < 0) {
port = HTTP_PORT;
}
if(is_local) {
host = LOCALHOST;
}
HttpRequest * request = MALLOC1(HttpRequest);
int init_code = HttpRequest_init(
request, method_code, host, url, body
);
if(check_error_init(init_code)) {
return NULL;
}
if(dict != NULL) {
PyObject *key, *value;
Py_ssize_t pos = 0;
while(PyDict_Next(dict, &pos, &key, &value)) {
PyObject * encoded_key = PyUnicode_AsEncodedString(
key, ENCODING_UTF8, "strict"
);
IF_NULL_THEN_NULL(encoded_key);
PyObject * value_str = IF_ELSE(
PyUnicode_Check(value), value, PyObject_Str(value)
);
IF_NULL_THEN_NULL(value_str);
PyObject * encoded_value = PyUnicode_AsEncodedString(
value_str, ENCODING_UTF8, "strict"
);
IF_NULL_THEN_NULL(encoded_value);
HttpRequest_add_header(
request,
PyBytes_AsString(encoded_key),
PyBytes_AsString(encoded_value)
);
printf("%s: %s\n", PyBytes_AsString(encoded_key), PyBytes_AsString(encoded_value));
Py_DECREF(encoded_key);
Py_DECREF(encoded_value);
}
}
byte_t * response = MALLOC_EMPTY(byte_t, 2 * HTTP_RESPONSE_SIZE);
int send_code = IF_ELSE(
is_local,
HttpRequest_send_port(request, response, port),
HttpRequest_send(request, response)
);
HttpRequest_destroy(request);
if(check_error_send(send_code)) {
FREE(response);
return NULL;
}
PyObject * response_py_str = PyUnicode_FromString((const char *) response);
FREE(response);
FREE(request);
return response_py_str;
}
static PyMethodDef myhttp_methods[] = {
{"get", py_get_request, METH_VARARGS, "Get Request"},
{"post", py_post_request, METH_VARARGS, "Post Request"},
{"head", py_head_request, METH_VARARGS, "Head Request"},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef myhttp_module = {
PyModuleDef_HEAD_INIT,
"MyHttp",
"Very simple Http client",
-1,
myhttp_methods
};
PyMODINIT_FUNC PyInit_myhttp() {
return PyModule_Create(&myhttp_module);
};