forked from haiwan/sledge
parent
8a3e050282
commit
aa0017f4b0
@ -0,0 +1,50 @@
|
||||
#ifndef SFRT_HTTP_H
|
||||
#define SFRT_HTTP_H
|
||||
|
||||
#include <http_parser.h>
|
||||
#include <types.h>
|
||||
#include <uv.h>
|
||||
|
||||
/* all in-memory ptrs.. don't mess around with that! */
|
||||
struct http_header {
|
||||
char *key;
|
||||
char *val;
|
||||
};
|
||||
|
||||
struct http_resp_header {
|
||||
char *hdr;
|
||||
int len;
|
||||
};
|
||||
|
||||
struct http_request {
|
||||
struct http_header headers[HTTP_HEADERS_MAX];
|
||||
int nheaders;
|
||||
char *body;
|
||||
int bodylen;
|
||||
// additional for http-parser
|
||||
int last_was_value;
|
||||
int header_end;
|
||||
int message_begin, message_end;
|
||||
};
|
||||
|
||||
struct http_response {
|
||||
struct http_resp_header headers[HTTP_HEADERS_MAX];
|
||||
int nheaders;
|
||||
char *body;
|
||||
int bodylen;
|
||||
char *status;
|
||||
int stlen;
|
||||
uv_buf_t bufs[HTTP_HEADERS_MAX * 2 + 3]; //max headers, one line for status code, remaining for body!
|
||||
};
|
||||
|
||||
int http_request_body_get(char **body);
|
||||
int http_request_parse(void);
|
||||
|
||||
int http_response_header_set(char *h, int len);
|
||||
int http_response_body_set(char *body, int len);
|
||||
int http_response_status_set(char *status, int len);
|
||||
int http_response_uv(void);
|
||||
|
||||
void http_init(void);
|
||||
|
||||
#endif /* SFRT_HTTP_H */
|
@ -0,0 +1,179 @@
|
||||
#include <http.h>
|
||||
#include <sandbox.h>
|
||||
#include <uv.h>
|
||||
|
||||
http_parser_settings settings;
|
||||
|
||||
static inline int
|
||||
http_on_msg_begin(http_parser *parser)
|
||||
{
|
||||
struct http_request *r = parser->data;
|
||||
|
||||
r->message_begin = 1;
|
||||
r->last_was_value = 1; //should always start with a header..
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
http_on_msg_end(http_parser *parser)
|
||||
{
|
||||
struct http_request *r = parser->data;
|
||||
|
||||
r->message_end = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
http_on_header_end(http_parser *parser)
|
||||
{
|
||||
struct http_request *r = parser->data;
|
||||
|
||||
r->header_end = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
http_on_url(http_parser* parser, const char *at, size_t length)
|
||||
{
|
||||
struct sandbox *s = sandbox_current();
|
||||
struct http_request *r = parser->data;
|
||||
|
||||
assert(strncmp(s->mod->name, (at + 1), length - 1) == 0);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
http_on_header_field(http_parser* parser, const char *at, size_t length)
|
||||
{
|
||||
struct http_request *r = parser->data;
|
||||
|
||||
if (r->last_was_value) r->nheaders ++;
|
||||
assert(r->nheaders <= HTTP_HEADERS_MAX);
|
||||
assert(length < HTTP_HEADER_MAXSZ);
|
||||
|
||||
r->last_was_value = 0;
|
||||
r->headers[r->nheaders - 1].key = (char *)at; //it is from the sandbox's req_resp_data, should persist.
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
http_on_header_value(http_parser* parser, const char *at, size_t length)
|
||||
{
|
||||
struct http_request *r = parser->data;
|
||||
|
||||
r->last_was_value = 1;
|
||||
assert(r->nheaders <= HTTP_HEADERS_MAX);
|
||||
assert(length < HTTP_HEADERVAL_MAXSZ);
|
||||
|
||||
r->headers[r->nheaders - 1].val = (char *)at; //it is from the sandbox's req_resp_data, should persist.
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static inline int
|
||||
http_on_body(http_parser* parser, const char *at, size_t length)
|
||||
{
|
||||
struct http_request *r = parser->data;
|
||||
struct sandbox *c = sandbox_current();
|
||||
|
||||
assert(length <= c->mod->max_req_sz);
|
||||
r->body = (char *)at;
|
||||
r->bodylen = length;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
http_request_body_get(char **b)
|
||||
{
|
||||
struct sandbox *s = sandbox_current();
|
||||
struct http_request *r = &s->rqi;
|
||||
|
||||
*b = r->body;
|
||||
return r->bodylen;
|
||||
}
|
||||
|
||||
int
|
||||
http_response_header_set(char *key, int len)
|
||||
{
|
||||
// by now, req_resp_data should only be containing response!
|
||||
struct sandbox *c = sandbox_current();
|
||||
struct http_response *r = &c->rsi;
|
||||
|
||||
assert(r->nheaders < HTTP_HEADERS_MAX);
|
||||
r->nheaders++;
|
||||
r->headers[r->nheaders-1].hdr = key;
|
||||
r->headers[r->nheaders-1].len = len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_response_body_set(char *body, int len)
|
||||
{
|
||||
struct sandbox *c = sandbox_current();
|
||||
struct http_response *r = &c->rsi;
|
||||
|
||||
assert(len < c->mod->max_resp_sz);
|
||||
r->body = body;
|
||||
r->bodylen = len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_response_status_set(char *status, int len)
|
||||
{
|
||||
struct sandbox *c = sandbox_current();
|
||||
struct http_response *r = &c->rsi;
|
||||
|
||||
r->status = status;
|
||||
r->stlen = len;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int http_response_uv(void)
|
||||
{
|
||||
struct sandbox *c = sandbox_current();
|
||||
struct http_response *r = &c->rsi;
|
||||
|
||||
int nb = 0;
|
||||
|
||||
r->bufs[nb] = uv_buf_init(r->status, r->stlen);
|
||||
nb++;
|
||||
for (int i = 0; i < r->nheaders; i++) {
|
||||
r->bufs[nb] = uv_buf_init(r->headers[i].hdr, r->headers[i].len);
|
||||
nb++;
|
||||
}
|
||||
|
||||
if (r->body) {
|
||||
r->bufs[nb] = uv_buf_init(r->body, r->bodylen);
|
||||
nb++;
|
||||
r->bufs[nb] = uv_buf_init(r->status + r->stlen - 2, 2); //for crlf
|
||||
nb++;
|
||||
}
|
||||
|
||||
return nb;
|
||||
}
|
||||
|
||||
int
|
||||
http_request_parse(void)
|
||||
{
|
||||
struct sandbox *s = sandbox_current();
|
||||
http_parser_execute(&s->hp, &settings, s->req_resp_data, s->rr_data_len);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void
|
||||
http_init(void)
|
||||
{
|
||||
http_parser_settings_init(&settings);
|
||||
settings.on_url = http_on_url;
|
||||
settings.on_header_field = http_on_header_field;
|
||||
settings.on_header_value = http_on_header_value;
|
||||
settings.on_body = http_on_body;
|
||||
settings.on_headers_complete = http_on_header_end;
|
||||
settings.on_message_begin = http_on_msg_begin;
|
||||
settings.on_message_complete = http_on_msg_end;
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
.PHONY: all client clean
|
||||
|
||||
all: clean client
|
||||
|
||||
client: client.c ../../http-parser/http_parser.c ../../http-parser/http_parser.h
|
||||
@echo "Compiling httpclient"
|
||||
@gcc -I../../http_parser/ ../../http-parser/http_parser.c client.c -g -o ../../bin/httpclient
|
||||
|
||||
clean:
|
||||
@rm -f ../../bin/httpclient
|
@ -0,0 +1,276 @@
|
||||
#include <sys/socket.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/wait.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define SERVER_PORT 10000
|
||||
#define SERVER_ADDR "127.0.0.1"
|
||||
|
||||
#define REQ_MAX 1024
|
||||
#define POST_MAX (1024*1024)
|
||||
#define RESP_MAX 1024
|
||||
//#define GET_REQUEST "GET %s HTTP/1.0\r\n"
|
||||
//#define GET_REQUEST "GET %s.ico HTTP/1.1\r\n" \
|
||||
"Host: 0.0.0.0=5000\r\n" \
|
||||
"User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9) Gecko/2008061015 Firefox/3.0\r\n" \
|
||||
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" \
|
||||
"Accept-Language: en-us,en;q=0.5\r\n" \
|
||||
"Accept-Encoding: gzip,deflate\r\n" \
|
||||
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" \
|
||||
"Keep-Alive: 300\r\n" \
|
||||
"Connection: keep-alive\r\n" \
|
||||
"\r\n"
|
||||
|
||||
//#define GET_REQUEST "GET /%s HTTP/1.1\r\n" \
|
||||
// "Host: 0.0.0.0=5000\r\n" \
|
||||
// "Accept: */*\r\n" \
|
||||
// "\r\n"
|
||||
#define GET_REQUEST "GET /%s HTTP/1.1\r\n" \
|
||||
"Host: 0.0.0.0=xxxx\r\n" \
|
||||
"\r\n"
|
||||
|
||||
#define POST_REQUEST "POST /%s HTTP/1.0\r\n" \
|
||||
"content-type: image/png\r\n" \
|
||||
"content-length: %lu\r\n" \
|
||||
"\r\n"
|
||||
|
||||
#define POST_REQUEST_END "\r\n\r\n"
|
||||
|
||||
|
||||
//Byte array of bitmap of 128 x 128 px:
|
||||
char img [] = {
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc1, 0x83, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3, 0xc0, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xf, 0xf0, 0x7f,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x1f, 0xfc,
|
||||
0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x7f,
|
||||
0xfe, 0xf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0,
|
||||
0xff, 0xff, 0x7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xc1, 0xff, 0xff, 0x83, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0x87, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xf, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xf, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe1, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc3, 0xff, 0xff, 0xff, 0xff, 0xc3, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xe1,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff,
|
||||
0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf, 0xff, 0xff, 0xff,
|
||||
0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff,
|
||||
0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f, 0xff,
|
||||
0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f,
|
||||
0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
|
||||
0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xf0, 0xf, 0xff, 0xff, 0xc7,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xff, 0xc0, 0x3, 0xff, 0xff,
|
||||
0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xff, 0x80, 0x1, 0xff,
|
||||
0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xfe, 0xf, 0xf0,
|
||||
0x7f, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xfe, 0x1f,
|
||||
0xf8, 0x7f, 0xff, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0xfc,
|
||||
0x7f, 0xfe, 0x3f, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff,
|
||||
0xf8, 0x7f, 0xfe, 0x1f, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f,
|
||||
0xff, 0xf8, 0xff, 0xff, 0x1f, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0x8f, 0xff, 0xf1, 0xff, 0xff, 0x8f, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0x8f, 0xff, 0xf1, 0xff, 0xff, 0x8f, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0x8f, 0xff, 0xf1, 0xff, 0xff, 0x8f, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0x8f, 0xff, 0xf1, 0xff, 0xff, 0x8f, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xf3, 0xff, 0xff, 0xcf, 0xff, 0xf8, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xf1, 0xff, 0xff, 0x8f, 0xff, 0xf8, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xf1, 0xff, 0xff, 0x8f, 0xff, 0xf8, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xf1, 0xff, 0xff, 0x8f, 0xff, 0xf8,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xf1, 0xff, 0xff, 0x8f, 0xff,
|
||||
0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xf8, 0xff, 0xff, 0x1f,
|
||||
0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xf8, 0x7f, 0xfe,
|
||||
0x1f, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x9f, 0xff, 0xfc, 0x3f,
|
||||
0xfc, 0x3f, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xfe,
|
||||
0x1f, 0xf8, 0x7f, 0xff, 0xf9, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff,
|
||||
0xff, 0x7, 0xe0, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f,
|
||||
0xff, 0xff, 0x80, 0x1, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0x8f, 0xff, 0xff, 0xe0, 0x7, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0x8f, 0xff, 0xff, 0xf8, 0x3f, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe1, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xf8, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xfc, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xfe, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
|
||||
0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xfe, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xfe, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x3f, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0x8f, 0xff, 0xf1, 0xff, 0xff, 0x9f, 0xff, 0xf1, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0xc0, 0xff, 0xff, 0x7, 0xff, 0xf1, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0x80, 0xff, 0xff, 0x3, 0xff, 0xf1, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0x80, 0x0, 0x0, 0x1, 0xff, 0xe1, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xff, 0x18, 0x0, 0x0, 0x10, 0xff, 0xe3,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xfe, 0x18, 0x0, 0x0, 0x18, 0x7f,
|
||||
0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xfc, 0x3f, 0xff, 0xff, 0xfc,
|
||||
0x7f, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xfc, 0x7f, 0xff, 0xff,
|
||||
0xfe, 0x3f, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xf8, 0xff, 0xff,
|
||||
0xff, 0xfe, 0x1f, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xf8, 0xff,
|
||||
0xff, 0xff, 0xff, 0x1f, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe3, 0xf1,
|
||||
0xff, 0xe0, 0x3f, 0xff, 0x8f, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf1,
|
||||
0xe1, 0xfc, 0x0, 0x1, 0xff, 0x8f, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xf1, 0xe3, 0xf0, 0x0, 0x0, 0x7f, 0xc7, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xf1, 0xc7, 0xf0, 0x3f, 0xe0, 0x3f, 0xc3, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xf1, 0xc7, 0xf1, 0xff, 0xfe, 0x3f, 0xe3, 0x8f, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xf8, 0x8f, 0xf1, 0xff, 0xfe, 0x7f, 0xf1, 0x1f, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xf8, 0xf, 0xf1, 0xff, 0xfc, 0x7f, 0xf1, 0x1f, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0x1f, 0xf1, 0xff, 0xfc, 0x7f, 0xf8, 0x1f, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xf9, 0xff, 0xfc, 0x7f, 0xf8, 0x1f, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x3f, 0xf8, 0xff, 0xfc, 0x7f, 0xfc, 0x3f,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f, 0xf8, 0xff, 0xf8, 0xff, 0xfe,
|
||||
0x3f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8, 0xff, 0xf8, 0xff,
|
||||
0xff, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0xff, 0xf8,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc, 0x7f,
|
||||
0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
|
||||
0x7f, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xfc, 0x7f, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xfe, 0x3f, 0xf1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xfe, 0x3f, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xfe, 0x3f, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xe3, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0x8f, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0xf, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc3, 0x1f, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x3f, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf0, 0x7f,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf8,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
};
|
||||
|
||||
int
|
||||
connect_n_send(void)
|
||||
{
|
||||
int sock;
|
||||
|
||||
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
perror("socket");
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct sockaddr_in srvaddr;
|
||||
srvaddr.sin_family = AF_INET;
|
||||
srvaddr.sin_port = htons(SERVER_PORT);
|
||||
if (inet_aton(SERVER_ADDR, &srvaddr.sin_addr) == 0) {
|
||||
perror("inet_addr");
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&srvaddr, sizeof(srvaddr)) < 0) {
|
||||
perror("connect");
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *req = (char *)malloc(REQ_MAX);
|
||||
assert(req);
|
||||
memset(req, 0, REQ_MAX);
|
||||
sprintf(req, GET_REQUEST, "empty");
|
||||
// char *req = (char *)malloc(POST_MAX);
|
||||
// assert(req);
|
||||
// memset(req, 0, POST_MAX);
|
||||
// sprintf(req, POST_REQUEST, "HELLO", sizeof(img));
|
||||
int len = strlen(req);
|
||||
// memcpy(req+len, img, sizeof(img));
|
||||
// strcat(req+len+sizeof(img), POST_REQUEST_END);
|
||||
// len += sizeof(img) + strlen(POST_REQUEST_END);
|
||||
char *resp = (char *)malloc(RESP_MAX);
|
||||
assert(resp);
|
||||
memset(resp, 0, RESP_MAX);
|
||||
|
||||
// int fd = open("tmp_cli.png", O_CREAT | O_RDWR | O_TRUNC, S_IRWXU | S_IRWXO | S_IRWXG);
|
||||
// if (fd < 0) {
|
||||
// perror("open");
|
||||
// goto skip;
|
||||
// }
|
||||
//
|
||||
// int sz = write(fd, img, sizeof(img));
|
||||
// if (sz < 0) {
|
||||
// perror("write");
|
||||
// }
|
||||
// close(fd);
|
||||
|
||||
skip:
|
||||
printf("Sending..\n");
|
||||
if (send(sock, req, len, 0) < 0) {
|
||||
perror("send");
|
||||
close(sock);
|
||||
return -1;
|
||||
}
|
||||
|
||||
printf("Receiving..\n");
|
||||
int r = 0, rcvd = 0;
|
||||
while ((r = recv(sock, resp + rcvd, RESP_MAX, 0)) > 0) {
|
||||
rcvd += r;
|
||||
if (r < RESP_MAX) break;
|
||||
resp = (char *)realloc(resp, rcvd + RESP_MAX);
|
||||
assert(resp);
|
||||
memset(resp + rcvd, 0, RESP_MAX);
|
||||
}
|
||||
|
||||
printf("Response: %s\n", resp);
|
||||
free(resp);
|
||||
free(req);
|
||||
close(sock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
while (1) {
|
||||
int s = 0;
|
||||
|
||||
printf("Test? (0 = exit)\n");
|
||||
scanf("%d", &s);
|
||||
if (s == 0) break;
|
||||
connect_n_send();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue