]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/serf/buckets/socket_buckets.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / serf / buckets / socket_buckets.c
1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <apr_pools.h>
17 #include <apr_network_io.h>
18
19 #include "serf.h"
20 #include "serf_private.h"
21 #include "serf_bucket_util.h"
22
23
24 typedef struct {
25     apr_socket_t *skt;
26
27     serf_databuf_t databuf;
28
29     /* Progress callback */
30     serf_progress_t progress_func;
31     void *progress_baton;
32 } socket_context_t;
33
34
35 static apr_status_t socket_reader(void *baton, apr_size_t bufsize,
36                                   char *buf, apr_size_t *len)
37 {
38     socket_context_t *ctx = baton;
39     apr_status_t status;
40
41     *len = bufsize;
42     status = apr_socket_recv(ctx->skt, buf, len);
43
44     if (status && !APR_STATUS_IS_EAGAIN(status))
45         serf__log_skt(SOCK_VERBOSE, __FILE__, ctx->skt,
46                       "socket_recv error %d\n", status);
47
48     if (*len)
49         serf__log_skt(SOCK_MSG_VERBOSE, __FILE__, ctx->skt,
50                       "--- socket_recv:\n%.*s\n-(%d)-\n",
51                       *len, buf, *len);
52
53     if (ctx->progress_func)
54         ctx->progress_func(ctx->progress_baton, *len, 0);
55
56     return status;
57 }
58
59 serf_bucket_t *serf_bucket_socket_create(
60     apr_socket_t *skt,
61     serf_bucket_alloc_t *allocator)
62 {
63     socket_context_t *ctx;
64
65     /* Oh, well. */
66     ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
67     ctx->skt = skt;
68
69     serf_databuf_init(&ctx->databuf);
70     ctx->databuf.read = socket_reader;
71     ctx->databuf.read_baton = ctx;
72
73     ctx->progress_func = NULL;
74     ctx->progress_baton = NULL;
75     return serf_bucket_create(&serf_bucket_type_socket, allocator, ctx);
76 }
77
78 void serf_bucket_socket_set_read_progress_cb(
79     serf_bucket_t *bucket,
80     const serf_progress_t progress_func,
81     void *progress_baton)
82 {
83     socket_context_t *ctx = bucket->data;
84
85     ctx->progress_func = progress_func;
86     ctx->progress_baton = progress_baton;
87 }
88
89 static apr_status_t serf_socket_read(serf_bucket_t *bucket,
90                                      apr_size_t requested,
91                                      const char **data, apr_size_t *len)
92 {
93     socket_context_t *ctx = bucket->data;
94
95     return serf_databuf_read(&ctx->databuf, requested, data, len);
96 }
97
98 static apr_status_t serf_socket_readline(serf_bucket_t *bucket,
99                                          int acceptable, int *found,
100                                          const char **data, apr_size_t *len)
101 {
102     socket_context_t *ctx = bucket->data;
103
104     return serf_databuf_readline(&ctx->databuf, acceptable, found, data, len);
105 }
106
107 static apr_status_t serf_socket_peek(serf_bucket_t *bucket,
108                                      const char **data,
109                                      apr_size_t *len)
110 {
111     socket_context_t *ctx = bucket->data;
112
113     return serf_databuf_peek(&ctx->databuf, data, len);
114 }
115
116 const serf_bucket_type_t serf_bucket_type_socket = {
117     "SOCKET",
118     serf_socket_read,
119     serf_socket_readline,
120     serf_default_read_iovec,
121     serf_default_read_for_sendfile,
122     serf_default_read_bucket,
123     serf_socket_peek,
124     serf_default_destroy_and_data,
125 };