-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevhttpconn.pyx
More file actions
114 lines (90 loc) · 4.23 KB
/
evhttpconn.pyx
File metadata and controls
114 lines (90 loc) · 4.23 KB
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
import re
from ev cimport *
from cpython cimport PyString_FromStringAndSize, PyString_AsStringAndSize
from cpython cimport Py_INCREF, Py_DECREF
cdef extern from "Python.h":
ctypedef int Py_ssize_t
cdef extern from "evhttpconn.h":
ctypedef struct evhttp_string_t:
char *data
int length
ctypedef void (*evhttp_connection_on_first_line)(evhttp_string_t first, evhttp_string_t second, evhttp_string_t third, void *data) except *
ctypedef void (*evhttp_connection_on_header)(evhttp_string_t key, evhttp_string_t value, void *data) except *
ctypedef void (*evhttp_connection_on_headers_end)(evhttp_string_t message, void *data) except *
ctypedef void (*evhttp_connection_on_content)(evhttp_string_t content, void *data) except *
ctypedef void (*evhttp_connection_on_complete)(void *data) except *
ctypedef void (*evhttp_connection_on_close)(void *data) except *
ctypedef struct evhttp_connection_t:
pass
void evhttp_connection_init(evhttp_connection_t *self,
ev_loop_t *loop,
int fd,
evhttp_connection_on_first_line on_first_line,
evhttp_connection_on_header on_header,
evhttp_connection_on_headers_end on_headers_end,
evhttp_connection_on_content on_chunk,
evhttp_connection_on_content on_complete_content,
evhttp_connection_on_complete on_complete,
evhttp_connection_on_close on_close,
void *callback_data)
void evhttp_connection_close(evhttp_connection_t *self)
int evhttp_connection_send(evhttp_connection_t *self, evhttp_string_t data)
void evhttp_connection_terminate(evhttp_connection_t *self)
cdef evhttp_string_t p2c(object str):
cdef evhttp_string_t res
cdef Py_ssize_t l
PyString_AsStringAndSize(str, <char **>&res.data, &l)
res.length = l
return res
cdef object c2p(evhttp_string_t str):
return PyString_FromStringAndSize(str.data, str.length)
cdef void on_first_line(evhttp_string_t first, evhttp_string_t second, evhttp_string_t third, void *data) except *:
(<Connection>data).on_first_line(c2p(first), c2p(second), c2p(third))
cdef void on_header(evhttp_string_t key, evhttp_string_t value, void *data) except *:
(<Connection>data).on_header(c2p(key), c2p(value))
cdef void on_headers_end(evhttp_string_t message, void *data) except *:
(<Connection>data).on_headers_end(c2p(message))
cdef void on_chunk(evhttp_string_t content, void *data) except *:
(<Connection>data).on_chunk(c2p(content))
cdef void on_complete(void *data) except *:
(<Connection>data).on_complete()
cdef void on_close(void *data) except *:
cdef Connection conn = <Connection>data
try:
conn.on_close()
finally:
Py_DECREF(conn)
cdef class Connection(object):
cdef evhttp_connection_t this
cdef object sock
def __init__(self, loop, sock):
self.sock = sock
Py_INCREF(self)
evhttp_connection_init(&self.this,
(<Loop>loop)._loop,
sock.fileno(),
on_first_line if self.on_first_line else <evhttp_connection_on_first_line>NULL,
on_header if self.on_header else <evhttp_connection_on_header>NULL,
on_headers_end if self.on_headers_end else <evhttp_connection_on_headers_end>NULL,
on_chunk if self.on_chunk else <evhttp_connection_on_content>NULL,
NULL,
on_complete if self.on_complete else <evhttp_connection_on_complete>NULL,
on_close if self.on_close else <evhttp_connection_on_close>NULL,
<void *>self)
def close(self):
evhttp_connection_close(&self.this)
def send(self, data):
if evhttp_connection_send(&self.this, p2c(data)) != 0:
self.close()
def terminate(self):
evhttp_connection_terminate(&self.this)
on_first_line = False
on_header = False
on_headers_end = False
on_chunk = False
on_complete = False
def on_close(self):
try:
self.sock.close()
except:
pass