-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsHttp.cpp
180 lines (137 loc) · 4.32 KB
/
JsHttp.cpp
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "JsHttp.h"
void JsHttpRequest::ParseUrl(char* url, char* host, int* port, char* path)
{
struct http_parser_url u;
if (0 == http_parser_parse_url(url, strlen(url), 0, &u)) {
if (u.field_set & (1 << UF_PORT))
*port = u.port;
else
*port = 80;
if (u.field_set & (1 << UF_HOST))
strncpy(host, url + u.field_data[UF_HOST].off, u.field_data[UF_HOST].len);
if (u.field_set & (1 << UF_PATH))
strncpy(path, url + u.field_data[UF_PATH].off, u.field_data[UF_PATH].len);
if (u.field_set & (1 << UF_QUERY)) {
strcat(path, "?");
strncpy(path + strlen(path), url + u.field_data[UF_QUERY].off, u.field_data[UF_QUERY].len);
}
}
}
void JsHttpRequest::CallBack(int status_code, const char* data)
{
jerry_api_value_t res;
jerry_api_value_t val[2];
val[0].type = JERRY_API_DATA_TYPE_UINT32;
val[0].v_uint32 = status_code;
val[1].type = JERRY_API_DATA_TYPE_STRING;
val[1].v_string = jerry_api_create_string((jerry_api_char_t*)data);
int is_ok = jerry_api_call_function(callback, NULL, &res, val, 2);
jerry_api_release_string(val[1].v_string);
}
int JsHttpRequest::OnBodyCallback(http_parser* parser, const char *at, size_t length)
{
JsHttpRequest* req = (JsHttpRequest*)parser->data;
req->CallBack(parser->status_code, at);
}
void JsHttpRequest::JsHttpReader(EV_P_ ev_io *w, int events)
{
JsHttpRequest* req = (JsHttpRequest*)w->priv;
req->Read(events);
}
void JsHttpRequest::Read(int events)
{
int size;
struct ev_loop* loop = ev_default_loop(0);
size = read(sock, buffer + buffer_index, HTTP_BUFFER_SIZE - buffer_index);
if (size == 0) {
http_parser_settings settings;
memset(&settings, 0, sizeof(settings));
settings.on_body = OnBodyCallback;
http_parser* parser = (http_parser*)malloc(sizeof(http_parser));
http_parser_init(parser, HTTP_RESPONSE);
parser->data = this;
size = http_parser_execute(parser, &settings, buffer, buffer_index);
if (parser->content_length == 0) {
//no body, OnBodyCallback will not called
//call js callback directly here
CallBack(parser->status_code, "");
}
ev_io_stop(loop, &watcher);
close(sock);
delete this;
} else if (size > 0) {
buffer_index += size;
}
}
int JsHttpRequest::SendRequest(char* host, short port, char* data, jerry_api_object_t* function)
{
struct sockaddr_in addr;
struct hostent* h;
int len = strlen(data);
int n = 0, total = 0;
callback = function;
struct ev_loop* loop = ev_default_loop(0);
h = gethostbyname(host);
if (!h)
return -errno;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
memcpy(&addr.sin_addr.s_addr, h->h_addr, h->h_length);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0) {
printf("create socket error\n");
return -errno;
}
if (connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
printf("connect to %s error\n", host);
return -errno;
}
ev_io_init(&watcher, JsHttpReader, sock, EV_READ);
watcher.priv = this;
ev_io_start(loop, &watcher);
do {
n = write(sock, data + total, len - total);
if (n < 0 && errno != EAGAIN)
break;
else if (n > 0)
total += n;
} while(total < len);
return total;
}
bool JsHttp::METHOD(Get)
{
char url[MAX_URL_LENGTH] = {0};
char host[MAX_URL_LENGTH] = {0};
char path[MAX_URL_LENGTH] = {0};
char req[MAX_URL_LENGTH] = {0};
int port;
if (args_cnt != 2 || !JVAL_IS_STRING(&args_p[0]) || !JVAL_IS_OBJECT(&args_p[1])) {
printf("http_get: invalid arguments\n");
return true;
}
jerry_api_string_to_char_buffer(args_p[0].v_string, (jerry_api_char_t*)url, MAX_URL_LENGTH);
JsHttpRequest::ParseUrl(url, host, &port, path);
sprintf(req, "GET %s HTTP/1.0\r\n\r\n", path);
JsHttpRequest* jsr = new JsHttpRequest();
int size = jsr->SendRequest(host, port, req, jerry_api_acquire_object(args_p[1].v_object));
if (size < 0) {
jsr->CallBack(500, "");
}
return true;
}
void JsHttp::Register()
{
JsHttp http;
http.SetMethod("get", Get);
}