Ejemplo n.º 1
0
HTTP_HDR_REQ *http_hdr_req_clone(const HTTP_HDR_REQ* hdr_req)
{
	HTTP_HDR_REQ *hh;

	hh = http_hdr_req_new();
	http_hdr_clone(&hdr_req->hdr, &hh->hdr);

	hh->port = hdr_req->port;
	ACL_SAFE_STRNCPY(hh->method, hdr_req->method, sizeof(hh->method));
	ACL_SAFE_STRNCPY(hh->host, hdr_req->host, sizeof(hh->host));
	acl_vstring_strcpy(hh->url_part, acl_vstring_str(hdr_req->url_part));
	acl_vstring_strcpy(hh->url_path, acl_vstring_str(hdr_req->url_path));
	acl_vstring_strcpy(hh->url_params, acl_vstring_str(hdr_req->url_params));
	acl_vstring_strcpy(hh->file_path, acl_vstring_str(hdr_req->file_path));

	if (hdr_req->params_table) {
		hh->params_table = acl_htable_create(__http_hdr_max_request, 0);
		acl_htable_walk(hdr_req->params_table, clone_table_entry,
				(void*) hh->params_table);
	}
	if (hdr_req->cookies_table) {
		hh->cookies_table = acl_htable_create(__http_hdr_max_cookies, 0);
		acl_htable_walk(hdr_req->cookies_table, clone_table_entry,
				(void*) hh->cookies_table);
	}

	return hh;
}
Ejemplo n.º 2
0
void http_client::reset(void)
{
	if (req_) {
		http_req_free(req_);
		req_ = NULL;
	} else if (hdr_req_)
		http_hdr_req_free(hdr_req_);

	hdr_req_ = http_hdr_req_new();
	json_.reset();
}
Ejemplo n.º 3
0
bool http_client::read_request_head(void)
{
	// 以防万一,先清除可能的上次请求的残留的中间数据对象
	reset();

	if (stream_ == NULL)
	{
		logger_error("client stream not open yet");
		disconnected_ = true;
		return false;
	}
	ACL_VSTREAM* vstream = stream_->get_vstream();
	if (vstream == NULL)
	{
		logger_error("client stream null");
		disconnected_ = true;
		return false;
	}

	hdr_req_ = http_hdr_req_new();
	int   ret = http_hdr_req_get_sync(hdr_req_, vstream, rw_timeout_);
	if (ret == -1)
	{
		http_hdr_req_free(hdr_req_);
		hdr_req_ = NULL;
		disconnected_ = true;
		return false;
	}

	if (http_hdr_req_parse(hdr_req_) < 0)
	{
		logger_error("parse request header error");
		http_hdr_req_free(hdr_req_);
		hdr_req_ = NULL;
		disconnected_ = true;
		return false;
	}

	if (hdr_req_->hdr.content_length <= 0)
		body_finish_ = true;
	return true;
}
Ejemplo n.º 4
0
HTTP_HDR_REQ *http_hdr_req_create(const char *url,
	const char *method, const char *version)
{
	const char *myname = "http_hdr_req_create";
	HTTP_HDR_REQ *hdr_req;
	ACL_VSTRING *req_line = acl_vstring_alloc(256);
	HTTP_HDR_ENTRY *entry;
	const char *ptr;
	static char *__user_agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0"
		"; zh-CN; rv:1.9.0.3) Gecko/2008092417 ACL/3.0.6";

	if (url == NULL || *url == 0) {
		acl_msg_error("%s(%d): url invalid", myname, __LINE__);
		return NULL;
	}
	if (method == NULL || *method == 0) {
		acl_msg_error("%s(%d): method invalid", myname, __LINE__);
		return NULL;
	}
	if (version == NULL || *version == 0) {
		acl_msg_error("%s(%d): version invalid", myname, __LINE__);
		return NULL;
	}

	acl_vstring_strcpy(req_line, method);
	acl_vstring_strcat(req_line, " ");

	if (strncasecmp(url, "http://", sizeof("http://") - 1) == 0)
		url += sizeof("http://") - 1;
	else if (strncasecmp(url, "https://", sizeof("https://") - 1) == 0)
		url += sizeof("https://") -1;
	ptr = strchr(url, '/');
	if (ptr)
		acl_vstring_strcat(req_line, ptr);
	else {
		ACL_VSTRING_ADDCH(req_line, '/');
		ACL_VSTRING_TERMINATE(req_line);
	}

	acl_vstring_strcat(req_line, " ");
	acl_vstring_strcat(req_line, version);

	entry = http_hdr_entry_new(acl_vstring_str(req_line));
	acl_vstring_free(req_line);

	if (entry == NULL) {
		acl_msg_error("%s(%d): http_hdr_entry_new return null for (%s)",
			myname, __LINE__, acl_vstring_str(req_line));
		return NULL;
	}

	hdr_req = http_hdr_req_new();
	http_hdr_append_entry(&hdr_req->hdr, entry);
	hdr_req->flag |= (HTTP_HDR_REQ_FLAG_PARSE_PARAMS | HTTP_HDR_REQ_FLAG_PARSE_COOKIE);
	if (http_hdr_req_line_parse(hdr_req) < 0) {
		http_hdr_req_free(hdr_req);
		return NULL;
	}

	hdr_req->host[0] = 0;
	__get_host_from_url(hdr_req->host, sizeof(hdr_req->host), url);
	if (hdr_req->host[0] != 0)
		http_hdr_put_str(&hdr_req->hdr, "Host", hdr_req->host);
	http_hdr_put_str(&hdr_req->hdr, "Connection", "Close");
	http_hdr_put_str(&hdr_req->hdr, "User-Agent", __user_agent);

	return hdr_req;
}
Ejemplo n.º 5
0
Archivo: main.c Proyecto: jhomble/redis
static void thread_run(void *arg)
{
    CONN *conn = (CONN*) arg;
    ACL_VSTREAM *client = conn->stream;
    const char *reply_200 = "HTTP/1.1 200 OK\r\n"
                            "Server: nginx/0.6.32\r\n"
                            "Date: Tue, 29 Dec 2009 02:18:25 GMT\r\n"
                            "Content-Type: text/html\r\n"
                            "Content-Length: 43\r\n"
                            "Last-Modified: Mon, 16 Nov 2009 02:18:14 GMT\r\n"
                            "Connection: keep-alive\r\n"
                            "Accept-Ranges: bytes\r\n\r\n"
                            "<html>\n"
                            "<body>\n"
                            "hello world!\n"
                            "</body>\n"
                            "</html>\n";
    int   ret, keep_alive;
    char  buf[4096];

    while (0) {
        ret = read(ACL_VSTREAM_SOCK(client), buf, sizeof(buf));
        if (ret == ACL_VSTREAM_EOF)
            break;
        ret = acl_vstream_writen(client, reply_200, strlen(reply_200));
        if (ret == ACL_VSTREAM_EOF)
            break;
    }

    while (0) {
        ret = acl_vstream_read(client, buf, sizeof(buf));
        if (ret == ACL_VSTREAM_EOF)
            break;
        ret = acl_vstream_writen(client, reply_200, strlen(reply_200));
        if (ret == ACL_VSTREAM_EOF)
            break;
    }

    while (0) {
        /*
        HTTP_REQ *req;
        */
        HTTP_HDR_REQ *hdr_req = http_hdr_req_new();

        ret = http_hdr_req_get_sync(hdr_req, client, 300);
        if (ret < 0) {
            http_hdr_req_free(hdr_req);
            break;
        }
        if (http_hdr_req_parse(hdr_req) < 0) {
            http_hdr_req_free(hdr_req);
            printf("parse error\n");
            break;
        }

        /*
        keep_alive = hdr_req->hdr.keep_alive;

        if (hdr_req->hdr.content_length > 0) {
        	req = http_req_new(hdr_req);
        	ret = (int) http_req_body_get_sync(req, client, buf, sizeof(buf));
        	if (ret < 0) {
        		http_req_free(req);
        		break;
        	}
        	http_req_free(req);
        } else {
        	http_hdr_req_free(hdr_req);
        }
        */

        http_hdr_req_free(hdr_req);
        ret = acl_vstream_writen(client, reply_200, strlen(reply_200));
        if (ret == ACL_VSTREAM_EOF) {
            break;
        }
        /*
        if (!keep_alive)
        	break;
        	*/
    }

    while (1) {
        HTTP_REQ *req;
        HTTP_HDR_REQ *hdr_req = http_hdr_req_new();

        ret = http_hdr_req_get_sync(hdr_req, client, 0);
        if (ret < 0) {
            http_hdr_req_free(hdr_req);
            break;
        }
        if (http_hdr_req_parse(hdr_req) < 0) {
            http_hdr_req_free(hdr_req);
            printf("parse error\n");
            break;
        }

        keep_alive = hdr_req->hdr.keep_alive;

        if (hdr_req->hdr.content_length > 0) {
            req = http_req_new(hdr_req);
            ret = (int) http_req_body_get_sync(req, client, buf, sizeof(buf));
            if (ret < 0) {
                http_req_free(req);
                break;
            }
            http_req_free(req);
        } else {
            http_hdr_req_free(hdr_req);
        }

        ret = acl_vstream_writen(client, reply_200, strlen(reply_200));
        if (ret == ACL_VSTREAM_EOF) {
            break;
        }
        if (!keep_alive)
            break;
    }

    acl_vstream_close(client);
    acl_myfree(conn);
    printf("thread(%ld) exit\n", (long) acl_pthread_self());
}