#if 0
//post方式从远程服务器下载一个文件
static struct event_base *base;
static void
http_request_file_done(struct evhttp_request *req, void *ctx)
{
char buffer[4097];
int nread;
printf("http_request_file_done");
if (req == NULL) {
/* If req is NULL, it means an error occurred, but
* sadly we are mostly left guessing what the error
* might have been. We'll do our best... */
struct bufferevent *bev = (struct bufferevent *) ctx;
unsigned long oslerr;
int printed_err = 0;
int errcode = EVUTIL_SOCKET_ERROR();
printf("some request failed - no idea which one though!\n");
/* Print out the OpenSSL error queue that libevent
* squirreled away for us, if any. */
if (!printed_err)
printf("socket error = %s (%d)\n",
evutil_socket_error_to_string(errcode),
errcode);
return;
}
printf("Response line: %d\n",
evhttp_request_get_response_code(req));
//unsigned char _down_data[1024];
while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req),
buffer, sizeof(buffer)-1))
> 0) {
/* These are just arbitrary chunks of 256 bytes.
* They are not lines, so we can't treat them as such. */
printf( "nread = %d\n", nread );
printf( "nreadq = %s\n", buffer );
printf( "buffer[0] = %x\n", buffer[0] );
printf( "buffer[1] = %x\n", buffer[1] );
printf( "buffer[2] = %x\n", buffer[2] );
printf( "buffer[3] = %x\n", buffer[3] );
buffer[nread] = 0;
//strcpy(&_gjson[_len], buffer);
//memcpy(&_download_file_buffer[_download_file_len], buffer, nread);
//_download_file_len += nread;
}
//_http_flag = 1;
event_base_loopbreak(base);
}
int http_get_file(char *_url, unsigned char *download_file_buffer)
{
int r;
unsigned char *_download_file_buffer;
_download_file_buffer = download_file_buffer;
struct evhttp_uri *http_uri = NULL;
char url[256], data_file[256];
const char *crt = NULL;
const char *scheme, *host, *path, *query;
char uri[256];
int port;
int retries = 0;
int timeout = -1;
struct bufferevent *bev;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req;
struct evkeyvalq *output_headers;
struct evbuffer *output_buffer;
int i;
int ret = 0;
enum { HTTP, HTTPS } type = HTTP;
char _filename[] = "post_file.c";
sprintf(url, "%s", _url);
sprintf(data_file, "%s%s", "C:\\Users\\zengyongheng\\Desktop\\", _filename);
http_uri = evhttp_uri_parse(url);
if (http_uri == NULL) {
printf("malformed url");
goto error;
}
scheme = evhttp_uri_get_scheme(http_uri);
if (scheme == NULL || (strcasecmp(scheme, "http") != 0 &&
strcasecmp(scheme, "http") != 0)) {
printf("url must be http or https");
goto error;
}
host = evhttp_uri_get_host(http_uri);
if (host == NULL) {
printf("url must have a host");
goto error;
}
port = evhttp_uri_get_port(http_uri);
if (port == -1) {
port = (strcasecmp(scheme, "http") == 0) ? 80 : 443;
}
path = evhttp_uri_get_path(http_uri);
if (strlen(path) == 0) {
path = "/";
}
query = evhttp_uri_get_query(http_uri);
if (query == NULL) {
sprintf(uri, "%s", path);
}
else {
sprintf(uri, "%s?%s", path, query);
}
uri[sizeof(uri)-1] = '\0';
// Create event base
base = event_base_new();
if (!base) {
perror("event_base_new()");
goto error;
}
// For simplicity, we let DNS resolution block. Everything else should be
// asynchronous though.
//evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev, host, port);
evcon = evhttp_connection_base_new(base, NULL, host, port);
if (evcon == NULL) {
fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
goto error;
}
if (retries > 0) {
evhttp_connection_set_retries(evcon, retries);
}
if (timeout >= 0) {
evhttp_connection_set_timeout(evcon, timeout);
}
//bev = evhttp_connection_get_bufferevent(evcon);
//if (bev == NULL) {
// printf("evhttp_connection_get_bufferevent() failed\n");
// goto error;
// }
// Fire off the request
req = evhttp_request_new(http_request_file_done, bev);
if (req == NULL) {
fprintf(stderr, "evhttp_request_new() failed\n");
goto error;
}
char tmp_buf[256] = { 0 };
output_headers = evhttp_request_get_output_headers(req);
evhttp_add_header(output_headers, "Host", host);
evhttp_add_header(output_headers, "Connection", "keep-alive");
r = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, uri);
if (r != 0) {
fprintf(stderr, "evhttp_make_request() failed\n");
goto error;
}
event_base_dispatch(base);
goto cleanup;
error:
ret = 1;
cleanup:
if (evcon)
evhttp_connection_free(evcon);
if (http_uri)
evhttp_uri_free(http_uri);
event_base_free(base);
return ret;
}
int main( int c, char **v )
{
int idx;
int reverse = 0, servertest = 0, use_getaddrinfo = 0;
struct event_base *event_base = NULL;
struct evdns_base *evdns_base = NULL;
if( c < 2 ){
fprintf(stderr, "syntax: %s [-x] [-v] hostname\n", v[0]);
fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
return 1;
}
idx = 1;
while (idx < c && v[idx][0] == '-') {
if (!strcmp(v[idx], "-x"))
reverse = 1;
else if (!strcmp(v[idx], "-v"))
verbose = 1;
else if (!strcmp(v[idx], "-g"))
use_getaddrinfo = 1;
else if (!strcmp(v[idx], "-servertest"))
servertest = 1;
else
fprintf(stderr, "Unknown option %s\n", v[idx]);
++idx;
}
#ifdef WIN32
{
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
}
#endif
http_get_file("http://172.16.1.18:8080/test.bin", "");
//http_get_(REQ_TEST0);
//fflush(stdout);
printf("debug4\n" );
return 0;
}
#endif
//post方式从远程服务器下载一个文件
static struct event_base *base;
static void
http_request_file_done(struct evhttp_request *req, void *ctx)
{
char buffer[4097];
int nread;
printf("http_request_file_done");
if (req == NULL) {
/* If req is NULL, it means an error occurred, but
* sadly we are mostly left guessing what the error
* might have been. We'll do our best... */
struct bufferevent *bev = (struct bufferevent *) ctx;
unsigned long oslerr;
int printed_err = 0;
int errcode = EVUTIL_SOCKET_ERROR();
printf("some request failed - no idea which one though!\n");
/* Print out the OpenSSL error queue that libevent
* squirreled away for us, if any. */
if (!printed_err)
printf("socket error = %s (%d)\n",
evutil_socket_error_to_string(errcode),
errcode);
return;
}
printf("Response line: %d\n",
evhttp_request_get_response_code(req));
//unsigned char _down_data[1024];
while ((nread = evbuffer_remove(evhttp_request_get_input_buffer(req),
buffer, sizeof(buffer)-1))
> 0) {
/* These are just arbitrary chunks of 256 bytes.
* They are not lines, so we can't treat them as such. */
printf( "nread = %d\n", nread );
printf( "nreadq = %s\n", buffer );
printf( "buffer[0] = %x\n", buffer[0] );
printf( "buffer[1] = %x\n", buffer[1] );
printf( "buffer[2] = %x\n", buffer[2] );
printf( "buffer[3] = %x\n", buffer[3] );
buffer[nread] = 0;
//strcpy(&_gjson[_len], buffer);
//memcpy(&_download_file_buffer[_download_file_len], buffer, nread);
//_download_file_len += nread;
}
//_http_flag = 1;
event_base_loopbreak(base);
}
int http_get_file(char *_url, unsigned char *download_file_buffer)
{
int r;
unsigned char *_download_file_buffer;
_download_file_buffer = download_file_buffer;
struct evhttp_uri *http_uri = NULL;
char url[256], data_file[256];
const char *crt = NULL;
const char *scheme, *host, *path, *query;
char uri[256];
int port;
int retries = 0;
int timeout = -1;
struct bufferevent *bev;
struct evhttp_connection *evcon = NULL;
struct evhttp_request *req;
struct evkeyvalq *output_headers;
struct evbuffer *output_buffer;
int i;
int ret = 0;
enum { HTTP, HTTPS } type = HTTP;
char _filename[] = "post_file.c";
sprintf(url, "%s", _url);
sprintf(data_file, "%s%s", "C:\\Users\\zengyongheng\\Desktop\\", _filename);
http_uri = evhttp_uri_parse(url);
if (http_uri == NULL) {
printf("malformed url");
goto error;
}
scheme = evhttp_uri_get_scheme(http_uri);
if (scheme == NULL || (strcasecmp(scheme, "http") != 0 &&
strcasecmp(scheme, "http") != 0)) {
printf("url must be http or https");
goto error;
}
host = evhttp_uri_get_host(http_uri);
if (host == NULL) {
printf("url must have a host");
goto error;
}
port = evhttp_uri_get_port(http_uri);
if (port == -1) {
port = (strcasecmp(scheme, "http") == 0) ? 80 : 443;
}
path = evhttp_uri_get_path(http_uri);
if (strlen(path) == 0) {
path = "/";
}
query = evhttp_uri_get_query(http_uri);
if (query == NULL) {
sprintf(uri, "%s", path);
}
else {
sprintf(uri, "%s?%s", path, query);
}
uri[sizeof(uri)-1] = '\0';
// Create event base
base = event_base_new();
if (!base) {
perror("event_base_new()");
goto error;
}
// For simplicity, we let DNS resolution block. Everything else should be
// asynchronous though.
//evcon = evhttp_connection_base_bufferevent_new(base, NULL, bev, host, port);
evcon = evhttp_connection_base_new(base, NULL, host, port);
if (evcon == NULL) {
fprintf(stderr, "evhttp_connection_base_bufferevent_new() failed\n");
goto error;
}
if (retries > 0) {
evhttp_connection_set_retries(evcon, retries);
}
if (timeout >= 0) {
evhttp_connection_set_timeout(evcon, timeout);
}
//bev = evhttp_connection_get_bufferevent(evcon);
//if (bev == NULL) {
// printf("evhttp_connection_get_bufferevent() failed\n");
// goto error;
// }
// Fire off the request
req = evhttp_request_new(http_request_file_done, bev);
if (req == NULL) {
fprintf(stderr, "evhttp_request_new() failed\n");
goto error;
}
char tmp_buf[256] = { 0 };
output_headers = evhttp_request_get_output_headers(req);
evhttp_add_header(output_headers, "Host", host);
evhttp_add_header(output_headers, "Connection", "keep-alive");
r = evhttp_make_request(evcon, req, EVHTTP_REQ_GET, uri);
if (r != 0) {
fprintf(stderr, "evhttp_make_request() failed\n");
goto error;
}
event_base_dispatch(base);
goto cleanup;
error:
ret = 1;
cleanup:
if (evcon)
evhttp_connection_free(evcon);
if (http_uri)
evhttp_uri_free(http_uri);
event_base_free(base);
return ret;
}
int main( int c, char **v )
{
int idx;
int reverse = 0, servertest = 0, use_getaddrinfo = 0;
struct event_base *event_base = NULL;
struct evdns_base *evdns_base = NULL;
if( c < 2 ){
fprintf(stderr, "syntax: %s [-x] [-v] hostname\n", v[0]);
fprintf(stderr, "syntax: %s [-servertest]\n", v[0]);
return 1;
}
idx = 1;
while (idx < c && v[idx][0] == '-') {
if (!strcmp(v[idx], "-x"))
reverse = 1;
else if (!strcmp(v[idx], "-v"))
verbose = 1;
else if (!strcmp(v[idx], "-g"))
use_getaddrinfo = 1;
else if (!strcmp(v[idx], "-servertest"))
servertest = 1;
else
fprintf(stderr, "Unknown option %s\n", v[idx]);
++idx;
}
#ifdef WIN32
{
WSADATA WSAData;
WSAStartup(0x101, &WSAData);
}
#endif
http_get_file("http://172.16.1.18:8080/test.bin", "");
//http_get_(REQ_TEST0);
//fflush(stdout);
printf("debug4\n" );
return 0;
}
#endif