summaryrefslogtreecommitdiffstats
path: root/src/core/transport/http/common/http_simple_request.c
blob: 9e10ce1e429150f207eabb1675360859adfeb461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <axis2_http_simple_request.h>
#include <axutil_array_list.h>
#include <axis2_http_transport.h>
#include <axutil_string.h>
#include <string.h>
#include <axutil_types.h>

struct axis2_http_simple_request
{
    axis2_http_request_line_t *request_line;
    axutil_array_list_t *header_group;
    axutil_stream_t *stream;
    axis2_bool_t owns_stream;
};

AXIS2_EXTERN axis2_http_simple_request_t *AXIS2_CALL
axis2_http_simple_request_create(
    const axutil_env_t * env,
    axis2_http_request_line_t * request_line,
    axis2_http_header_t ** http_headers,
    axis2_ssize_t http_hdr_count,
    axutil_stream_t * content)
{
    axis2_http_simple_request_t *simple_request = NULL;
    simple_request = (axis2_http_simple_request_t *)AXIS2_MALLOC(env->allocator,
        sizeof(axis2_http_simple_request_t));
    if(!simple_request)
    {
        AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        return NULL;
    }

    memset((void *)simple_request, 0, sizeof(axis2_http_simple_request_t));
    simple_request->request_line = request_line;
    simple_request->stream = content;

    if(!(simple_request->stream))
    {
        simple_request->stream = axutil_stream_create_basic(env);
        if(!simple_request->stream)
        {
            axis2_http_simple_request_free(simple_request, env);
            AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
            return NULL;
        }
        simple_request->owns_stream = AXIS2_TRUE;
    }

    if((http_hdr_count > 0) && http_headers)
    {
        int i = 0;
        simple_request->header_group = axutil_array_list_create(env, http_hdr_count);

        for(i = 0; i < (int)http_hdr_count; i++)
        /* We are sure that the difference lies within the int range */
        {
            axutil_array_list_add(simple_request->header_group, env, (void *)http_headers[i]);
        }
    }

    return simple_request;
}

AXIS2_EXTERN void AXIS2_CALL
axis2_http_simple_request_free(
    axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env)
{
    /* free the stream only if we own it. Otherwise shouldn't free the stream
     * since it belongs to the socket
     */
    if(simple_request->owns_stream)
    {
        axutil_stream_free(simple_request->stream, env);
    }

    if(simple_request->request_line)
    {
        axis2_http_request_line_free(simple_request->request_line, env);
    }

    if(simple_request->header_group)
    {
        int i = 0;
        axis2_http_header_t *tmp = NULL;
        for(i = 0; i < axutil_array_list_size(simple_request->header_group, env); i++)
        {
            tmp = (axis2_http_header_t*)axutil_array_list_get(simple_request->header_group, env, i);
            axis2_http_header_free(tmp, env);
        }
        axutil_array_list_free(simple_request->header_group, env);
    }
    AXIS2_FREE(env->allocator, simple_request);
}

AXIS2_EXTERN axis2_http_request_line_t *AXIS2_CALL
axis2_http_simple_request_get_request_line(
    const axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env)
{
    return simple_request->request_line;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_http_simple_request_set_request_line(
    axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env,
    axis2_http_request_line_t * request_line)
{
    AXIS2_PARAM_CHECK(env->error, request_line, AXIS2_FAILURE);
    simple_request->request_line = request_line;
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axis2_http_simple_request_contains_header(
    axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env,
    const axis2_char_t * name)
{
    int i = 0;
    axis2_char_t *header_name = NULL;
    int count = 0;

    AXIS2_PARAM_CHECK(env->error, name, AXIS2_FAILURE);

    if(!simple_request->header_group)
    {
        AXIS2_LOG_WARNING(env->log, AXIS2_LOG_SI,
            "http simple request \
does not contain headers, unable to find: %s header", name);
        return AXIS2_FALSE;
    }

    count = axutil_array_list_size(simple_request->header_group, env);

    if(0 == count)
    {
        AXIS2_LOG_WARNING(env->log, AXIS2_LOG_SI,
            "http simple request \
contains zero headers, unable to find: %s header", name);
        return AXIS2_FALSE;
    }

    for(i = 0; i < count; i++)
    {
        header_name = axis2_http_header_get_name((axis2_http_header_t *)axutil_array_list_get(
            simple_request->header_group, env, i), env);
        if(0 == axutil_strcasecmp(name, header_name))
        {
            return AXIS2_TRUE;
        }
    }
    return AXIS2_FALSE;
}

AXIS2_EXTERN axutil_array_list_t *AXIS2_CALL
axis2_http_simple_request_get_headers(
    const axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env)
{
    return simple_request->header_group;
}

AXIS2_EXTERN axis2_http_header_t *AXIS2_CALL
axis2_http_simple_request_get_first_header(
    const axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env,
    const axis2_char_t * str)
{
    axutil_array_list_t *header_group = NULL;
    int i = 0;
    int count = 0;

    AXIS2_PARAM_CHECK(env->error, str, NULL);

    header_group = simple_request->header_group;
    if(!simple_request->header_group)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
            "http simple request does not contain any headers; unable to find: %s header", str);
        return NULL;
    }

    if(0 == axutil_array_list_size(header_group, env))
    {
        AXIS2_LOG_WARNING(env->log, AXIS2_LOG_SI,
            "http simple request contain zero headers, unable to find: %s header", str);
        return NULL;
    }

    count = axutil_array_list_size(header_group, env);
    for(i = 0; i < count; i++)
    {
        axis2_http_header_t *tmp_header = NULL;
        axis2_char_t *tmp_name = NULL;
        tmp_header = (axis2_http_header_t *)axutil_array_list_get(header_group, env, i);
        tmp_name = axis2_http_header_get_name(tmp_header, env);
        if(0 == axutil_strcasecmp(str, tmp_name))
        {
            return tmp_header;
        }
    }
    return NULL;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_http_simple_request_remove_headers(
    axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env,
    const axis2_char_t * str)
{
    axis2_http_header_t *tmp_header = NULL;
    axis2_char_t *tmp_name = NULL;
    int i = 0;
    int count = 0;
    axutil_array_list_t *header_group = NULL;

    AXIS2_PARAM_CHECK(env->error, str, AXIS2_FAILURE);

    header_group = simple_request->header_group;

    if(!header_group)
    {
        /* Even though we couldn't complete the op, we are sure that the
         * requred header is no more in the request. So we can proceed without a
         * problem.
         */
        return AXIS2_SUCCESS;
    }

    count = axutil_array_list_size(header_group, env);

    for(i = 0; i < count; i++)
    {
        tmp_header = (axis2_http_header_t *)axutil_array_list_get(header_group, env, i);
        tmp_name = axis2_http_header_get_name(tmp_header, env);
        if(0 == axutil_strcasecmp(str, tmp_name))
        {
            axis2_http_header_free(tmp_header, env);
            axutil_array_list_remove(header_group, env, i);
            break;
        }
    }
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_http_simple_request_add_header(
    axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env,
    axis2_http_header_t * header)
{
    AXIS2_PARAM_CHECK(env->error, header, AXIS2_FAILURE);

    if(!simple_request->header_group)
    {
        simple_request->header_group = axutil_array_list_create(env, 1);
    }
    return axutil_array_list_add(simple_request->header_group, env, header);
}

AXIS2_EXTERN const axis2_char_t *AXIS2_CALL
axis2_http_simple_request_get_content_type(
    const axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env)
{
    axis2_http_header_t *tmp_header = NULL;

    tmp_header = axis2_http_simple_request_get_first_header(simple_request, env,
        AXIS2_HTTP_HEADER_CONTENT_TYPE);
    if(tmp_header)
    {
        return axis2_http_header_get_value(tmp_header, env);
    }

    return AXIS2_HTTP_HEADER_ACCEPT_TEXT_PLAIN;
}

AXIS2_EXTERN const axis2_char_t *AXIS2_CALL
axis2_http_simple_request_get_charset(
    const axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env)
{
    axis2_http_header_t *tmp_header = NULL;

    tmp_header = axis2_http_simple_request_get_first_header(simple_request, env,
        AXIS2_HTTP_HEADER_CONTENT_TYPE);
    if(tmp_header)
    {
        axis2_char_t *value = axis2_http_header_get_value(tmp_header, env);
        axis2_char_t *charset = (axis2_char_t *)strstr((char *)value,
            (char *)AXIS2_HTTP_CHAR_SET_ENCODING);
        if(charset)
        {
            charset = strchr((char *)charset, AXIS2_EQ);
            return charset;
        }
    }

    return AXIS2_HTTP_DEFAULT_CONTENT_CHARSET;
}

AXIS2_EXTERN axis2_ssize_t AXIS2_CALL
axis2_http_simple_request_get_content_length(
    const axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env)
{
    axis2_http_header_t *tmp_header = NULL;
    int error_return = -1;

    tmp_header = axis2_http_simple_request_get_first_header(simple_request, env,
        AXIS2_HTTP_HEADER_CONTENT_LENGTH);
    if(tmp_header)
    {
        return AXIS2_ATOI(axis2_http_header_get_value(tmp_header, env));
    }
    return error_return;
}

AXIS2_EXTERN axutil_stream_t *AXIS2_CALL
axis2_http_simple_request_get_body(
    const axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env)
{
    return simple_request->stream;
}

AXIS2_EXTERN axis2_ssize_t AXIS2_CALL
axis2_http_simple_request_get_body_bytes(
    const axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env,
    char **buf)
{
    axutil_stream_t *body = NULL;
    char *tmp_buf = NULL;
    char *tmp_buf2 = NULL;
    char *tmp_buf3 = NULL;
    int length = 0;
    int read_len = 0;

    body = simple_request->stream;
    if(!body)
    {
        *buf = (char *)AXIS2_MALLOC(env->allocator, 1);
        *buf[0] = '\0';
        return 0;
    }

    length = axis2_http_simple_request_get_content_length(simple_request, env);
    if(length > 0)
    {
        *buf = (char *)AXIS2_MALLOC(env->allocator, length + 1);
        read_len = axutil_stream_read(body, env, *buf, length + 1);
        return read_len;
    }

    tmp_buf2 = AXIS2_MALLOC(env->allocator, 128 * sizeof(char));
    while(axutil_stream_read(body, env, tmp_buf2, 128) > 0)
    {
        tmp_buf3 = axutil_stracat(env, tmp_buf, tmp_buf2);
        if(tmp_buf)
        {
            AXIS2_FREE(env->allocator, tmp_buf);
            tmp_buf = NULL;
        }
        tmp_buf = tmp_buf3;

    }

    if(tmp_buf2)
    {
        AXIS2_FREE(env->allocator, tmp_buf2);
        tmp_buf2 = NULL;
    }

    if(tmp_buf)
    {
        *buf = tmp_buf;
        return axutil_strlen(tmp_buf);
    }

    *buf = (char *)AXIS2_MALLOC(env->allocator, 1);
    *buf[0] = AXIS2_ESC_NULL;
    return 0;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_http_simple_request_set_body_string(
    axis2_http_simple_request_t * simple_request,
    const axutil_env_t * env,
    void *str,
    unsigned int str_len)
{
    axutil_stream_t *body_stream = NULL;

    AXIS2_PARAM_CHECK(env->error, str, AXIS2_FAILURE);

    body_stream = simple_request->stream;
    if(!body_stream)
    {
        body_stream = axutil_stream_create_basic(env);
        if(!body_stream)
        {
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI,
                "unable to create stream\
for stream %s of %d length", (axis2_char_t *)str, str_len);
            return AXIS2_FAILURE;
        }
        simple_request->stream = body_stream;
        simple_request->owns_stream = AXIS2_TRUE;
    }

    axutil_stream_write(body_stream, env, str, str_len);
    return AXIS2_SUCCESS;
}