Example #1
0
int http_session_create(struct http_server_t *server, socket_t socket, const struct sockaddr* sa, socklen_t salen)
{
	struct http_session_t *session;
	struct aio_tcp_transport_handler_t handler;
	memset(&handler, 0, sizeof(handler));
	handler.ondestroy = http_session_ondestroy;
	handler.onrecv = http_session_onrecv;
	handler.onsend = http_session_onsend;

	session = (struct http_session_t *)malloc(sizeof(*session) + 2 * 1024 + HTTP_RECV_BUFFER);
	if (!session) return -1;

	memset(session, 0, sizeof(*session));
	session->header = (char*)(session + 1);
	session->header_capacity = 2 * 1024;
	session->data = session->header + session->header_capacity;
	session->parser = http_parser_create(HTTP_PARSER_SERVER);
	assert(AF_INET == sa->sa_family || AF_INET6 == sa->sa_family);
	assert(salen <= sizeof(session->addr));
	memcpy(&session->addr, sa, salen);
	session->addrlen = salen;
	session->socket = socket;
	session->param = server->param;
	session->handler = server->handler;
	session->transport = aio_tcp_transport_create(socket, &handler, session);
	if (0 != aio_tcp_transport_recv(session->transport, session->data, HTTP_RECV_BUFFER))
	{
		aio_tcp_transport_destroy(session->transport);
		return -1;
	}
	return 0;
}
void
transport_http_factory0(http_handler_f hdl, struct network_client_s *client)
{
	struct network_transport_s *transport;
	struct transport_client_context_s *client_context;

	client_context = g_malloc0(sizeof(struct transport_client_context_s));
	client_context->handler = hdl;
	client_context->parser = http_parser_create();
	client_context->request = http_request_create(client);

	transport = &(client->transport);
	transport->client_context = client_context;
	transport->clean_context = http_context_clean;
	transport->notify_input = http_notify_input;
	transport->notify_error = NULL;

	network_client_allow_input(client, TRUE);
}