mirror of
https://gitee.com/acl-dev/acl.git
synced 2024-12-05 05:18:53 +08:00
205 lines
4.8 KiB
C
205 lines
4.8 KiB
C
#include "lib_acl.h"
|
||
#include "lib_protocol.h"
|
||
#include "service.h"
|
||
#include "http_service.h"
|
||
|
||
void http_client_free(CLIENT_ENTRY *entry)
|
||
{
|
||
HTTP_CLIENT *client = (HTTP_CLIENT*) entry;
|
||
|
||
while (1) {
|
||
HTTP_CLIENT_REQ *req =(HTTP_CLIENT_REQ*)
|
||
acl_fifo_pop(&client->req_list);
|
||
if (req == NULL)
|
||
break;
|
||
http_client_req_free(req);
|
||
}
|
||
|
||
if (client->req_curr) {
|
||
http_client_req_free(client->req_curr);
|
||
client->req_curr = NULL;
|
||
}
|
||
|
||
if (client->res) {
|
||
http_res_free(client->res);
|
||
client->res = NULL;
|
||
client->hdr_res = NULL;
|
||
} else if (client->hdr_res) {
|
||
http_hdr_res_free(client->hdr_res);
|
||
client->hdr_res = NULL;
|
||
}
|
||
if (client->fp) {
|
||
acl_vstream_close(client->fp);
|
||
client->fp = NULL;
|
||
}
|
||
if (client->buf) {
|
||
acl_vstring_free(client->buf);
|
||
client->buf = NULL;
|
||
}
|
||
|
||
client_entry_free(entry);
|
||
}
|
||
|
||
HTTP_CLIENT *http_client_new(HTTP_SERVICE *service, ACL_ASTREAM *stream)
|
||
{
|
||
HTTP_CLIENT *client;
|
||
|
||
client = (HTTP_CLIENT*) client_entry_new((SERVICE*) service,
|
||
sizeof(HTTP_CLIENT), stream);
|
||
client->req_curr = NULL;
|
||
acl_fifo_init(&client->req_list);
|
||
client->entry.free_fn = http_client_free;
|
||
client->flag = 0;
|
||
|
||
return (client);
|
||
}
|
||
|
||
void http_client_reset(HTTP_CLIENT *client)
|
||
{
|
||
while (1) {
|
||
HTTP_CLIENT_REQ *req =(HTTP_CLIENT_REQ*)
|
||
acl_fifo_pop(&client->req_list);
|
||
if (req == NULL)
|
||
break;
|
||
http_client_req_free(req);
|
||
}
|
||
client->req_curr = NULL;
|
||
|
||
if (client->hdr_res) {
|
||
http_hdr_res_free(client->hdr_res);
|
||
client->hdr_res = NULL;
|
||
}
|
||
if (client->res) {
|
||
client->res->hdr_res = NULL;
|
||
http_res_free(client->res);
|
||
client->res = NULL;
|
||
}
|
||
if (client->fp) {
|
||
acl_vstream_close(client->fp);
|
||
client->fp = NULL;
|
||
}
|
||
if (client->buf)
|
||
ACL_VSTRING_RESET(client->buf);
|
||
if (client->cache)
|
||
client->cache = NULL;
|
||
client->flag = 0;
|
||
}
|
||
|
||
HTTP_CLIENT_REQ *http_client_req_new(HTTP_CLIENT *http_client)
|
||
{
|
||
HTTP_CLIENT_REQ *req = (HTTP_CLIENT_REQ*)
|
||
acl_mycalloc(1, sizeof(HTTP_CLIENT_REQ));
|
||
req->http_client = http_client;
|
||
return (req);
|
||
}
|
||
|
||
void http_client_req_free(HTTP_CLIENT_REQ *req)
|
||
{
|
||
if (req->req)
|
||
http_req_free(req->req);
|
||
else if (req->hdr_req)
|
||
http_hdr_req_free(req->hdr_req);
|
||
acl_myfree(req);
|
||
}
|
||
|
||
int http_client_req_filter(HTTP_CLIENT *http_client)
|
||
{
|
||
HTTP_SERVICE *service = (HTTP_SERVICE*) http_client->entry.service;
|
||
HTTP_HDR_REQ *hdr_req;
|
||
HTTP_PLUGIN *plugin = NULL;
|
||
void *plugin_req_ctx;
|
||
ACL_ASTREAM *astream;
|
||
ACL_VSTREAM *stream;
|
||
ACL_ITER iter;
|
||
int ret = 0;
|
||
|
||
/* xxx: plugin_req_ctx <20>ò<EFBFBD><C3B2><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>п<EFBFBD><D0BF>ܲ<EFBFBD>һ<EFBFBD><D2BB>, <20><><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><D0B9><EFBFBD> */
|
||
http_client->plugin_req_ctx = NULL;
|
||
|
||
/* <20><><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||
|
||
acl_foreach(iter, &service->request_plugins) {
|
||
HTTP_PLUGIN *tmp = (HTTP_PLUGIN*) iter.data;
|
||
astream = http_client->entry.client;
|
||
stream = acl_aio_vstream(astream);
|
||
|
||
if ((ret = tmp->filter.request(stream,
|
||
http_client->req_curr->hdr_req,
|
||
&http_client->plugin_req_ctx)) != 0)
|
||
{
|
||
plugin = tmp;
|
||
break;
|
||
}
|
||
}
|
||
|
||
/* <20><><EFBFBD><EFBFBD> 0 <20><>ʾ<EFBFBD>û<EFBFBD><C3BB>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><D7BC><EFBFBD>ӹܸ<D3B9><DCB8><EFBFBD><EFBFBD><EFBFBD> */
|
||
if (plugin == NULL || ret == 0)
|
||
return (0);
|
||
|
||
astream = http_client->entry.client;
|
||
stream = acl_aio_vstream(astream);
|
||
|
||
if (ret < 0) {
|
||
/* <20><><EFBFBD>ظ<EFBFBD>ֵ<EFBFBD><D6B5>ʾ<EFBFBD><CABE>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><F3A3ACBF><EFBFBD>Ϊ<EFBFBD><CEAA>-4xx, -5xx */
|
||
|
||
acl_aio_refer(astream);
|
||
|
||
switch (-ret) {
|
||
case 403:
|
||
acl_aio_writen(astream, HTTP_REQUEST_DENY,
|
||
(int) strlen(HTTP_REQUEST_DENY));
|
||
break;
|
||
case 404:
|
||
acl_aio_writen(astream, HTTP_REQUEST_NOFOUND,
|
||
(int) strlen(HTTP_REQUEST_NOFOUND));
|
||
break;
|
||
case 500:
|
||
default:
|
||
acl_aio_writen(astream, HTTP_INTERNAL_ERROR,
|
||
(int) strlen(HTTP_INTERNAL_ERROR));
|
||
break;
|
||
}
|
||
|
||
acl_aio_unrefer(astream);
|
||
|
||
http_client_req_free(http_client->req_curr);
|
||
http_client->req_curr = NULL;
|
||
client_entry_detach(&http_client->entry, stream);
|
||
acl_aio_disable_readwrite(astream);
|
||
acl_aio_clean_hooks(astream);
|
||
acl_aio_iocp_close(astream);
|
||
return (-1);
|
||
}
|
||
|
||
hdr_req = http_client->req_curr->hdr_req;
|
||
http_client->req_curr->hdr_req = NULL;
|
||
|
||
plugin_req_ctx = http_client->plugin_req_ctx;
|
||
|
||
/* <20>ͷŵ<CDB7><C5B5>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD>صĶ<D8B5><C4B6><EFBFBD>, <20><><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ô<EFBFBD><C3B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,
|
||
* <20><>Ϊ<EFBFBD><CEAA>ʱΪ<CAB1><CEAA><EFBFBD><EFBFBD>ʱ<EFBFBD>Ĺ<EFBFBD><C4B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD>Ӧ<EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD>
|
||
* <20><><EFBFBD><EFBFBD> client_entry_detach <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD>ü<EFBFBD><C3BC><EFBFBD>Ϊ0<CEAA><30><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>
|
||
* http_client <20>ͷ<EFBFBD>
|
||
*/
|
||
client_entry_detach(&http_client->entry, stream);
|
||
|
||
/* <20>رն<D8B1><D5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>¼<EFBFBD> */
|
||
acl_aio_disable_read(astream);
|
||
/* <20><><EFBFBD><EFBFBD><EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD> */
|
||
acl_aio_clean_hooks(astream);
|
||
|
||
/* <20><><EFBFBD>ͻ<EFBFBD><CDBB><EFBFBD><EFBFBD>첽<EFBFBD><ECB2BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD> */
|
||
acl_aio_ctl(astream, ACL_AIO_CTL_STREAM, NULL, ACL_AIO_CTL_END);
|
||
|
||
/* xxx: <20>첽<EFBFBD>ر<EFBFBD> astream <20>첽<EFBFBD><ECB2BD> */
|
||
acl_aio_iocp_close(astream);
|
||
|
||
/* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD><EFBFBD><EFBFBD>ģʽת<CABD><D7AA>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>ģʽ */
|
||
acl_non_blocking(ACL_VSTREAM_SOCK(stream), ACL_BLOCKING);
|
||
|
||
/* ת<><D7AA><EFBFBD>û<EFBFBD><C3BB>Զ<EFBFBD><D4B6>崦<EFBFBD><E5B4A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||
plugin->forward.request(stream, hdr_req, plugin_req_ctx);
|
||
return (1);
|
||
}
|
||
|