1 /* ====================================================================
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
18 * ====================================================================
21 #include <apr_pools.h>
23 #include <apr_version.h>
26 #include "serf_bucket_util.h"
28 #include "serf_private.h"
30 static apr_status_t read_from_client(serf_incoming_t *client)
35 static apr_status_t write_to_client(serf_incoming_t *client)
40 apr_status_t serf__process_client(serf_incoming_t *client, apr_int16_t events)
43 if ((events & APR_POLLIN) != 0) {
44 rv = read_from_client(client);
50 if ((events & APR_POLLHUP) != 0) {
51 return APR_ECONNRESET;
54 if ((events & APR_POLLERR) != 0) {
58 if ((events & APR_POLLOUT) != 0) {
59 rv = write_to_client(client);
68 apr_status_t serf__process_listener(serf_listener_t *l)
73 /* THIS IS NOT OPTIMAL */
74 apr_pool_create(&p, l->pool);
76 rv = apr_socket_accept(&in, l->skt, p);
83 rv = l->accept_func(l->ctx, l, l->accept_baton, in, p);
94 apr_status_t serf_incoming_create(
95 serf_incoming_t **client,
99 serf_incoming_request_cb_t request,
103 serf_incoming_t *ic = apr_palloc(pool, sizeof(*ic));
106 ic->baton.type = SERF_IO_CLIENT;
107 ic->baton.u.client = ic;
108 ic->request_baton = request_baton;
109 ic->request = request;
111 ic->desc.desc_type = APR_POLL_SOCKET;
112 ic->desc.desc.s = ic->skt;
113 ic->desc.reqevents = APR_POLLIN;
115 rv = ctx->pollset_add(ctx->pollset_baton,
116 &ic->desc, &ic->baton);
123 apr_status_t serf_listener_create(
124 serf_listener_t **listener,
129 serf_accept_client_t accept,
134 serf_listener_t *l = apr_palloc(pool, sizeof(*l));
137 l->baton.type = SERF_IO_LISTENER;
138 l->baton.u.listener = l;
139 l->accept_func = accept;
140 l->accept_baton = accept_baton;
142 apr_pool_create(&l->pool, pool);
144 rv = apr_sockaddr_info_get(&sa, host, APR_UNSPEC, port, 0, l->pool);
148 rv = apr_socket_create(&l->skt, sa->family,
150 #if APR_MAJOR_VERSION > 0
157 rv = apr_socket_opt_set(l->skt, APR_SO_REUSEADDR, 1);
161 rv = apr_socket_bind(l->skt, sa);
165 rv = apr_socket_listen(l->skt, 5);
169 l->desc.desc_type = APR_POLL_SOCKET;
170 l->desc.desc.s = l->skt;
171 l->desc.reqevents = APR_POLLIN;
173 rv = ctx->pollset_add(ctx->pollset_baton,
174 &l->desc, &l->baton);