1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ajp.h"
18
19APLOG_USE_MODULE(proxy_ajp);
20
21apr_status_t ajp_ilink_send(apr_socket_t *sock, ajp_msg_t *msg)
22{
23    char         *buf;
24    apr_status_t status;
25    apr_size_t   length;
26
27    ajp_msg_end(msg);
28
29    length = msg->len;
30    buf    = (char *)msg->buf;
31
32    do {
33        apr_size_t written = length;
34
35        status = apr_socket_send(sock, buf, &written);
36        if (status != APR_SUCCESS) {
37            ap_log_error(APLOG_MARK, APLOG_ERR, status, NULL, APLOGNO(01029)
38                          "ajp_ilink_send(): send failed");
39            return status;
40        }
41        length -= written;
42        buf    += written;
43    } while (length);
44
45    return APR_SUCCESS;
46}
47
48
49static apr_status_t ilink_read(apr_socket_t *sock, apr_byte_t *buf,
50                               apr_size_t len)
51{
52    apr_size_t   length = len;
53    apr_size_t   rdlen  = 0;
54    apr_status_t status;
55
56    while (rdlen < len) {
57
58        status = apr_socket_recv(sock, (char *)(buf + rdlen), &length);
59
60        if (status == APR_EOF)
61            return status;          /* socket closed. */
62        else if (APR_STATUS_IS_EAGAIN(status))
63            continue;
64        else if (status != APR_SUCCESS)
65            return status;          /* any error. */
66
67        rdlen += length;
68        length = len - rdlen;
69    }
70    return APR_SUCCESS;
71}
72
73
74apr_status_t ajp_ilink_receive(apr_socket_t *sock, ajp_msg_t *msg)
75{
76    apr_status_t status;
77    apr_size_t   hlen;
78    apr_size_t   blen;
79
80    hlen = msg->header_len;
81
82    status = ilink_read(sock, msg->buf, hlen);
83
84    if (status != APR_SUCCESS) {
85        ap_log_error(APLOG_MARK, APLOG_ERR, status, NULL, APLOGNO(01030)
86                     "ajp_ilink_receive() can't receive header");
87        return (APR_STATUS_IS_TIMEUP(status) ? APR_TIMEUP : AJP_ENO_HEADER);
88    }
89
90    status = ajp_msg_check_header(msg, &blen);
91
92    if (status != APR_SUCCESS) {
93        ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, APLOGNO(01031)
94                     "ajp_ilink_receive() received bad header");
95        return AJP_EBAD_HEADER;
96    }
97
98    status = ilink_read(sock, msg->buf + hlen, blen);
99
100    if (status != APR_SUCCESS) {
101        ap_log_error(APLOG_MARK, APLOG_ERR, status, NULL, APLOGNO(01032)
102                     "ajp_ilink_receive() error while receiving message body "
103                     "of length %" APR_SIZE_T_FMT,
104                     hlen);
105        return AJP_EBAD_MESSAGE;
106    }
107
108    ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, NULL, APLOGNO(01033)
109                 "ajp_ilink_receive() received packet len=%" APR_SIZE_T_FMT
110                 "type=%d",
111                  blen, (int)msg->buf[hlen]);
112
113    return APR_SUCCESS;
114}
115
116