Esempio n. 1
0
int http_client::on_head(int status, void* ctx)
{
	http_client* hc = (http_client*) ctx;

	acl_aio_disable_readwrite(hc->conn_);

	if (status != HTTP_CHAT_OK) {
		logger_error("invalid status=%d", status);
		acl_aio_iocp_close(hc->conn_);
		return -1;
	}

	if (http_hdr_req_parse(hc->hdr_req_) < 0) {
		logger_error("parse http header error");
		acl_aio_iocp_close(hc->conn_);
		return -1;
	}

	hc->content_length_ = hc->hdr_req_->hdr.content_length;
	if (hc->content_length_ <= 0)
		return hc->handle() ? 0 : -1;

	hc->req_ = http_req_new(hc->hdr_req_);
	http_req_body_get_async(hc->req_, hc->conn_, on_body,
		hc, hc->rw_timeout_);

	return 0;
}
Esempio n. 2
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;
}
Esempio n. 3
0
File: main.c Progetto: 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());
}