Example #1
0
int
pmhttpClientFetch(http_client *cp, const char *url,
		char *body_buffer, size_t body_length,
		char *type_buffer, size_t type_length)
{
    int		sts = 0, redirected = 0;

    if (pmDebug & DBG_TRACE_HTTP)
	fprintf(stderr, "pmhttpClientFetch: %s\n", url);

    if (http_client_prepare(cp, url, body_buffer, body_length,
			    type_buffer, type_length) != 0)
	return -1;

    while (redirected <= cp->max_redirect) {
	/* ensure we're connected to the server */
	if ((sts = http_client_connect(cp)) < 0)
	    return sts;

	/* make a GET request via the given URL */
	if ((sts = http_client_get(cp)) < 0)
	    return sts;
	if (sts == 1)	/* server connection is lost */
	    continue;

	/* parse, extract body, handle redirect */
	if ((sts = http_client_response(cp)) < 0) {
	    if (sts == -EAGAIN)		/* server closed */
		continue;
	    if (sts == -EMLINK) {	/* http redirect */
		redirected++;
		continue;
	    }
	    break;	/* propogate errors */
	}
	break;	/* successful exchange */
    }

    if (pmDebug & DBG_TRACE_HTTP)
	fprintf(stderr, "pmhttpClientFetch sts=%d\n", sts);

    return sts;
}
Example #2
0
/*
 * response_write: 回写http响应。 
 * 参数: 
 *		client: client实例的指针,
 *		sockfd: socket 文件描述符。
 * 返回: 
 *		-1: 表示出错,
 *		1(HTTP_SOCKFD_IN): 表示I/O层监听sockfd读事件。
 *		2(HTTP_SOCKFD_OUT): 表示I/O层监听sockfd写事件。
 *		4(HTTP_SOCKFD_DEL): 表示I/O层删除sockfd事件监听。
 */
int response_write(struct http_client *client, int sockfd)
{
	struct pool_entry *entry;
	int ret = -1;
	unsigned int	epflag = 0;

	if ( NULL == client || sockfd < 0 )
		return ret;

	if ( !client->inprocess ) {
		/* 不应该会执行到此处 */
		fprintf(stderr, "WRITE: client->inprocess equal 0 and sockfd equal %d\n",
				sockfd);
		return ret;
	}

	ret = http_client_response(client, &epflag);
	if (ret < 0)
		return ret;
	if ( HTTP_SOCKFD_OUT == epflag ) {
		return HTTP_SOCKFD_OUT;
	} else if ( HTTP_SOCKFD_IN == epflag ) {
		return HTTP_SOCKFD_IN;
	} else if ( HTTP_SOCKFD_DEL == epflag ) {
		return HTTP_SOCKFD_DEL;
	} else {
		/* 出错了 */
		return -1;
	}

	/* TODO */

	ret = http_client_keepalive(client);
	if ( ret && ret != -1 ) {
		/* 准备下一次连接请求 */
		http_client_reset(client);
		return HTTP_SOCKFD_IN;
	} else {
		return HTTP_SOCKFD_DEL;
	}
}