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>
22 #include <apr_network_io.h>
25 #include "serf_private.h"
26 #include "serf_bucket_util.h"
32 serf_databuf_t databuf;
34 /* Progress callback */
35 serf_progress_t progress_func;
40 static apr_status_t socket_reader(void *baton, apr_size_t bufsize,
41 char *buf, apr_size_t *len)
43 socket_context_t *ctx = baton;
47 status = apr_socket_recv(ctx->skt, buf, len);
49 if (status && !APR_STATUS_IS_EAGAIN(status))
50 serf__log_skt(SOCK_VERBOSE, __FILE__, ctx->skt,
51 "socket_recv error %d\n", status);
54 serf__log_skt(SOCK_MSG_VERBOSE, __FILE__, ctx->skt,
55 "--- socket_recv:\n%.*s\n-(%d)-\n",
58 if (ctx->progress_func && *len)
59 ctx->progress_func(ctx->progress_baton, *len, 0);
64 serf_bucket_t *serf_bucket_socket_create(
66 serf_bucket_alloc_t *allocator)
68 socket_context_t *ctx;
71 ctx = serf_bucket_mem_alloc(allocator, sizeof(*ctx));
74 serf_databuf_init(&ctx->databuf);
75 ctx->databuf.read = socket_reader;
76 ctx->databuf.read_baton = ctx;
78 ctx->progress_func = NULL;
79 ctx->progress_baton = NULL;
80 return serf_bucket_create(&serf_bucket_type_socket, allocator, ctx);
83 void serf_bucket_socket_set_read_progress_cb(
84 serf_bucket_t *bucket,
85 const serf_progress_t progress_func,
88 socket_context_t *ctx = bucket->data;
90 ctx->progress_func = progress_func;
91 ctx->progress_baton = progress_baton;
94 static apr_status_t serf_socket_read(serf_bucket_t *bucket,
96 const char **data, apr_size_t *len)
98 socket_context_t *ctx = bucket->data;
100 return serf_databuf_read(&ctx->databuf, requested, data, len);
103 static apr_status_t serf_socket_readline(serf_bucket_t *bucket,
104 int acceptable, int *found,
105 const char **data, apr_size_t *len)
107 socket_context_t *ctx = bucket->data;
109 return serf_databuf_readline(&ctx->databuf, acceptable, found, data, len);
112 static apr_status_t serf_socket_peek(serf_bucket_t *bucket,
116 socket_context_t *ctx = bucket->data;
118 return serf_databuf_peek(&ctx->databuf, data, len);
121 const serf_bucket_type_t serf_bucket_type_socket = {
124 serf_socket_readline,
125 serf_default_read_iovec,
126 serf_default_read_for_sendfile,
127 serf_default_read_bucket,
129 serf_default_destroy_and_data,