~ubuntu-branches/ubuntu/oneiric/libapache-mod-jk/oneiric

« back to all changes in this revision

Viewing changes to jk/native/common/jk_sockbuf.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2006-08-05 16:30:53 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060805163053-myf66gm6j1a21ps6
Tags: 1:1.2.18-1ubuntu1
Merge from Debian unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright 1999-2004 The Apache Software Foundation
3
 
 *
4
 
 *  Licensed under the Apache License, Version 2.0 (the "License");
5
 
 *  you may not use this file except in compliance with the License.
6
 
 *  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
 
/***************************************************************************
18
 
 * Description: Simple buffer object to handle buffered socket IO          *
19
 
 * Author:      Gal Shachor <shachor@il.ibm.com>                           *
20
 
 * Version:     $Revision: 1.10 $                                           *
21
 
 ***************************************************************************/
22
 
 
23
 
#include "jk_global.h"
24
 
#include "jk_sockbuf.h"
25
 
 
26
 
static int fill_buffer(jk_sockbuf_t *sb);
27
 
 
28
 
int jk_sb_open(jk_sockbuf_t *sb, int sd)
29
 
{
30
 
    if (sb && sd >= 0) {
31
 
        sb->end = 0;
32
 
        sb->start = 0;
33
 
        sb->sd = sd;
34
 
        return JK_TRUE;
35
 
    }
36
 
 
37
 
    return JK_FALSE;
38
 
}
39
 
 
40
 
int jk_sb_write(jk_sockbuf_t *sb, const void *buf, unsigned sz)
41
 
{
42
 
    if (sb && buf && sz) {
43
 
        if ((SOCKBUF_SIZE - sb->end) >= sz) {
44
 
            memcpy(sb->buf + sb->end, buf, sz);
45
 
            sb->end += sz;
46
 
        }
47
 
        else {
48
 
            if (!jk_sb_flush(sb)) {
49
 
                return JK_FALSE;
50
 
            }
51
 
            if (sz > SOCKBUF_SIZE) {
52
 
                return (send(sb->sd, (char *)buf, sz, 0) == (int)sz);
53
 
            }
54
 
 
55
 
            memcpy(sb->buf + sb->end, buf, sz);
56
 
            sb->end += sz;
57
 
        }
58
 
 
59
 
        return JK_TRUE;
60
 
    }
61
 
 
62
 
    return JK_FALSE;
63
 
}
64
 
 
65
 
int jk_sb_flush(jk_sockbuf_t *sb)
66
 
{
67
 
    if (sb) {
68
 
        int save_out = sb->end;
69
 
        sb->end = sb->start = 0;
70
 
        if (save_out) {
71
 
            return send(sb->sd, sb->buf, save_out, 0) == save_out;
72
 
        }
73
 
        return JK_TRUE;
74
 
    }
75
 
 
76
 
    return JK_FALSE;
77
 
}
78
 
 
79
 
 
80
 
int jk_sb_read(jk_sockbuf_t *sb, char **buf, unsigned sz, unsigned *ac)
81
 
{
82
 
    if (sb && buf && ac) {
83
 
        unsigned avail;
84
 
 
85
 
        *ac = 0;
86
 
        *buf = NULL;
87
 
 
88
 
        if (sb->end == sb->start) {
89
 
            sb->end = sb->start = 0;
90
 
            if (fill_buffer(sb) < 0) {
91
 
                return JK_FALSE;
92
 
            }
93
 
        }
94
 
 
95
 
        *buf = sb->buf + sb->start;
96
 
        avail = sb->end - sb->start;
97
 
        if (avail > sz) {
98
 
            *ac = sz;
99
 
        }
100
 
        else {
101
 
            *ac = avail;
102
 
        }
103
 
        sb->start += *ac;
104
 
 
105
 
        return JK_TRUE;
106
 
    }
107
 
 
108
 
    return JK_FALSE;
109
 
}
110
 
 
111
 
int jk_sb_gets(jk_sockbuf_t *sb, char **ps)
112
 
{
113
 
    int ret;
114
 
    if (sb) {
115
 
        while (1) {
116
 
            unsigned i;
117
 
            for (i = sb->start; i < sb->end; i++) {
118
 
                if (JK_LF == sb->buf[i]) {
119
 
                    if (i > sb->start && JK_CR == sb->buf[i - 1]) {
120
 
                        sb->buf[i - 1] = '\0';
121
 
                    }
122
 
                    else {
123
 
                        sb->buf[i] = '\0';
124
 
                    }
125
 
                    *ps = sb->buf + sb->start;
126
 
                    sb->start = (i + 1);
127
 
                    return JK_TRUE;
128
 
                }
129
 
            }
130
 
            if ((ret = fill_buffer(sb)) < 0) {
131
 
                return JK_FALSE;
132
 
            }
133
 
            else if (ret == 0) {
134
 
                *ps = sb->buf + sb->start;
135
 
                if ((SOCKBUF_SIZE - sb->end) > 0) {
136
 
                    sb->buf[sb->end] = '\0';
137
 
                }
138
 
                else {
139
 
                    sb->buf[sb->end - 1] = '\0';
140
 
                }
141
 
                return JK_TRUE;
142
 
            }
143
 
        }
144
 
    }
145
 
 
146
 
    return JK_FALSE;
147
 
}
148
 
 
149
 
/*
150
 
 * Read data from the socket into the associated buffer, and update the
151
 
 * start and end indices.  May move the data currently in the buffer.  If
152
 
 * new data is read into the buffer (or if it is already full), returns 1.
153
 
 * If EOF is received on the socket, returns 0.  In case of error returns
154
 
 * -1.  
155
 
 */
156
 
static int fill_buffer(jk_sockbuf_t *sb)
157
 
{
158
 
    int ret;
159
 
 
160
 
    /*
161
 
     * First move the current data to the beginning of the buffer
162
 
     */
163
 
    if (sb->start < sb->end) {
164
 
        if (sb->start > 0) {
165
 
            unsigned to_copy = sb->end - sb->start;
166
 
            memmove(sb->buf, sb->buf + sb->start, to_copy);
167
 
            sb->start = 0;
168
 
            sb->end = to_copy;
169
 
        }
170
 
    }
171
 
    else {
172
 
        sb->start = sb->end = 0;
173
 
    }
174
 
 
175
 
    /*
176
 
     * In the unlikely case where the buffer is already full, we won't be
177
 
     * reading anything and we'd be calling recv with a 0 count.  
178
 
     */
179
 
    if ((SOCKBUF_SIZE - sb->end) > 0) {
180
 
        /*
181
 
         * Now, read more data
182
 
         */
183
 
        ret = recv(sb->sd, sb->buf + sb->end, SOCKBUF_SIZE - sb->end, 0);
184
 
 
185
 
        /* 0 is EOF/SHUTDOWN, -1 is SOCK_ERROR */
186
 
        if (ret <= 0) {
187
 
            return ret;
188
 
        }
189
 
 
190
 
        sb->end += ret;
191
 
    }
192
 
 
193
 
    return 1;
194
 
}