1 /* Copyright 2002-2004 Justin Erenkrantz and Greg Stein
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <apr_pools.h>
17 #include <apr_network_io.h>
20 #include "serf_private.h"
21 #include "serf_bucket_util.h"
27 serf_databuf_t databuf;
29 /* Progress callback */
30 serf_progress_t progress_func;
35 static apr_status_t socket_reader(void *baton, apr_size_t bufsize,
36 char *buf, apr_size_t *len)
38 socket_context_t *ctx = baton;
42 status = apr_socket_recv(ctx->skt, buf, len);
44 if (status && !APR_STATUS_IS_EAGAIN(status))
45 serf__log_skt(SOCK_VERBOSE, __FILE__, ctx->skt,
46 "socket_recv error %d\n", status);
49 serf__log_skt(SOCK_MSG_VERBOSE, __FILE__, ctx->skt,
50 "--- socket_recv:\n%.*s\n-(%d)-\n",
53 if (ctx->progress_func)
54 ctx->progress_func(ctx->progress_baton, *len, 0);
59 serf_bucket_t *serf_bucket_socket_create(
61 serf_bucket_alloc_t *allocator)
63 socket_context_t *ctx;
66 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
69 serf_databuf_init(&ctx->databuf);
70 ctx->databuf.read = socket_reader;
71 ctx->databuf.read_baton = ctx;
73 ctx->progress_func = NULL;
74 ctx->progress_baton = NULL;
75 return serf_bucket_create(&serf_bucket_type_socket, allocator, ctx);
78 void serf_bucket_socket_set_read_progress_cb(
79 serf_bucket_t *bucket,
80 const serf_progress_t progress_func,
83 socket_context_t *ctx = bucket->data;
85 ctx->progress_func = progress_func;
86 ctx->progress_baton = progress_baton;
89 static apr_status_t serf_socket_read(serf_bucket_t *bucket,
91 const char **data, apr_size_t *len)
93 socket_context_t *ctx = bucket->data;
95 return serf_databuf_read(&ctx->databuf, requested, data, len);
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)
102 socket_context_t *ctx = bucket->data;
104 return serf_databuf_readline(&ctx->databuf, acceptable, found, data, len);
107 static apr_status_t serf_socket_peek(serf_bucket_t *bucket,
111 socket_context_t *ctx = bucket->data;
113 return serf_databuf_peek(&ctx->databuf, data, len);
116 const serf_bucket_type_t serf_bucket_type_socket = {
119 serf_socket_readline,
120 serf_default_read_iovec,
121 serf_default_read_for_sendfile,
122 serf_default_read_bucket,
124 serf_default_destroy_and_data,