summaryrefslogtreecommitdiffstats
path: root/src/core/transport/http/common/simple_http_svr_conn.c
blob: 958be28458abad369c929aa0e3e600cdc7d9bca0 (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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
/*
 * 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_simple_http_svr_conn.h>
#include <axis2_http_transport.h>
#include <axutil_string.h>
#include <axutil_network_handler.h>
#include <axutil_http_chunked_stream.h>
#include <platforms/axutil_platform_auto_sense.h>
#include <string.h>
#include <axis2_http_simple_response.h>
#include <axis2_http_transport_utils.h>

struct axis2_simple_http_svr_conn
{
    int socket;
    axutil_stream_t *stream;
    axis2_bool_t keep_alive;
};

static axis2_char_t *
axis2_simple_http_svr_conn_read_line(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env);

AXIS2_EXTERN axis2_simple_http_svr_conn_t *AXIS2_CALL
axis2_simple_http_svr_conn_create(
    const axutil_env_t * env,
    int sockfd)
{
    axis2_simple_http_svr_conn_t *svr_conn = NULL;
    svr_conn = (axis2_simple_http_svr_conn_t *)AXIS2_MALLOC(env->allocator,
        sizeof(axis2_simple_http_svr_conn_t));
    if(!svr_conn)
    {
        AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_NO_MEMORY, AXIS2_FAILURE);
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "server connection failed. Insufficient memory");
        return NULL;
    }

    memset((void *)svr_conn, 0, sizeof(axis2_simple_http_svr_conn_t));
    svr_conn->socket = sockfd;

    if(-1 != svr_conn->socket)
    {
        svr_conn->stream = axutil_stream_create_socket(env, svr_conn->socket);
        if(!svr_conn->stream)
        {
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "socket creation failed, socket %d", sockfd);
            axis2_simple_http_svr_conn_free(svr_conn, env);
            return NULL;
        }
    }
    return svr_conn;
}

AXIS2_EXTERN void AXIS2_CALL
axis2_simple_http_svr_conn_free(
    axis2_simple_http_svr_conn_t *svr_conn,
    const axutil_env_t * env)
{
    axis2_simple_http_svr_conn_close(svr_conn, env);
    AXIS2_FREE(env->allocator, svr_conn);
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_simple_http_svr_conn_close(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    axutil_stream_free(svr_conn->stream, env);
    if(-1 != svr_conn->socket)
    {
        axutil_network_handler_close_socket(env, svr_conn->socket);
        svr_conn->socket = -1;
    }
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axis2_simple_http_svr_conn_is_open(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    if(-1 != svr_conn->socket)
    {
        return AXIS2_TRUE;
    }
    return AXIS2_FALSE;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_simple_http_svr_conn_set_keep_alive(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env,
    axis2_bool_t keep_alive)
{
    svr_conn->keep_alive = keep_alive;
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_bool_t AXIS2_CALL
axis2_simple_http_svr_conn_is_keep_alive(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    return svr_conn->keep_alive;
}

AXIS2_EXTERN axutil_stream_t *AXIS2_CALL
axis2_simple_http_svr_conn_get_stream(
    const axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    return svr_conn->stream;
}

AXIS2_EXTERN axis2_http_response_writer_t *AXIS2_CALL
axis2_simple_http_svr_conn_get_writer(
    const axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    return axis2_http_response_writer_create(env, svr_conn->stream);
}

AXIS2_EXTERN axis2_http_simple_request_t *AXIS2_CALL
axis2_simple_http_svr_conn_read_request(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    axis2_char_t* str_line = NULL;
    axis2_bool_t end_of_headers = AXIS2_FALSE;
    axis2_http_request_line_t *request_line = NULL;
    axis2_http_simple_request_t *request = NULL;

    /* read first line of the request (which is <HTTP METHOD> <URI> <HTTP VERSION> CRLF */
    str_line = axis2_simple_http_svr_conn_read_line(svr_conn, env);
    if(str_line)
    {
        request_line = axis2_http_request_line_parse_line(env, str_line);
        AXIS2_FREE(env->allocator, str_line);
        str_line = NULL;
    }

    if(!request_line)
    {
        AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_INVALID_HTTP_HEADER_START_LINE, AXIS2_FAILURE);
        return NULL;
    }
    request = axis2_http_simple_request_create(env, request_line, NULL, 0, svr_conn->stream);

    /* now read the headers until we find a line only having CRLF */
    while(AXIS2_FALSE == end_of_headers)
    {
        str_line = axis2_simple_http_svr_conn_read_line(svr_conn, env);
        if(!str_line)
        {
            /*if nothing is read, this loop should be broken. Otherwise, going to be endless loop */
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "reading http header failed");
            break;
        }

        if(0 == axutil_strcmp(str_line, AXIS2_HTTP_CRLF))
        {
            /* line contains only CRLF, so should be end of headers */
            end_of_headers = AXIS2_TRUE;
        }
        else
        {
            axis2_http_header_t *tmp_header = axis2_http_header_create_by_str(env, str_line);
            if(tmp_header)
            {
                axis2_http_simple_request_add_header(request, env, tmp_header);
            }
        }

        AXIS2_FREE(env->allocator, str_line);
        str_line = NULL;
    }
    return request;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_simple_http_svr_conn_write_response(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env,
    axis2_http_simple_response_t * response)
{
    axis2_http_response_writer_t *response_writer = NULL;
    axutil_array_list_t *headers = NULL;
    axutil_stream_t *response_stream = NULL;
    axis2_char_t *response_body = NULL;
    int body_size = 0;

    axis2_http_header_t *enc_header = NULL;
    axis2_bool_t chuked_encoding = AXIS2_FALSE;
    axis2_char_t *status_line = NULL;
    axis2_bool_t binary_content = AXIS2_FALSE;
    axis2_char_t *content_type = NULL;

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

    response_writer = axis2_http_response_writer_create(env, svr_conn->stream);
    if(!response_writer)
    {
        AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Cannot create http response writer");
        return AXIS2_FAILURE;
    }

    content_type = (axis2_char_t *)axis2_http_simple_response_get_content_type(response, env);
    if(content_type)
    {
        if(strstr(content_type, AXIS2_HTTP_HEADER_ACCEPT_MULTIPART_RELATED)
            && strstr(content_type,AXIS2_HTTP_HEADER_ACCEPT_XOP_XML))
        {
            binary_content = AXIS2_TRUE;
        }
    }

    enc_header = axis2_http_simple_response_get_first_header(response, env,
        AXIS2_HTTP_HEADER_TRANSFER_ENCODING);
    if(enc_header)
    {
        axis2_char_t *enc_value = axis2_http_header_get_value(enc_header, env);
        if(enc_value && (0 == axutil_strcmp(enc_value, AXIS2_HTTP_HEADER_TRANSFER_ENCODING_CHUNKED)))
        {
            chuked_encoding = AXIS2_TRUE;

            /* remove the content length header */
            axis2_http_simple_response_remove_headers(response, env,
                AXIS2_HTTP_HEADER_CONTENT_LENGTH);
        }
    }

    /* print status line */
    status_line = axis2_http_simple_response_get_status_line(response, env);
    if(!status_line)
    {
        AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_INVALID_HTTP_HEADER_START_LINE, AXIS2_FAILURE);
        axis2_http_response_writer_free(response_writer, env);
        return AXIS2_FAILURE;
    }
    axis2_http_response_writer_print_str(response_writer, env, status_line);

    headers = axis2_http_simple_response_get_headers(response, env);
    if(headers)
    {
        int i = 0;
        int count = axutil_array_list_size(headers, env);
        for(; i < count; i++)
        {
            axis2_http_header_t *header =
                (axis2_http_header_t *)axutil_array_list_get(headers, env, i);
            if(header)
            {
                axis2_char_t *header_ext_form = axis2_http_header_to_external_form(header, env);
                axis2_http_response_writer_print_str(response_writer, env, header_ext_form);
                AXIS2_FREE(env->allocator, header_ext_form);
            }
        }
    }

    /* write empty line after http headers */
    axis2_http_response_writer_print_str(response_writer, env, AXIS2_HTTP_CRLF);

    /* write the body */
    response_stream = axis2_http_simple_response_get_body(response, env);
    if(response_stream)
    {
        body_size = axutil_stream_get_len(response_stream, env);
        response_body = axutil_stream_get_buffer(response_stream, env);
        axutil_stream_flush_buffer(response_stream, env);
        response_body[body_size] = AXIS2_ESC_NULL;
    }

    if(body_size <= 0 && !binary_content)
    {
        /* no body available to write. Note that this is not an error. We might want to write only
         * status information and hence, this is a valid case */
        axis2_http_response_writer_free(response_writer, env);
        return AXIS2_SUCCESS;
    }

    if(!chuked_encoding && !binary_content)
    {
        /* This sending a normal SOAP response without chunk transfer encoding */
        axis2_status_t write_stat = AXIS2_FAILURE;
        write_stat = axis2_http_response_writer_println_str(response_writer, env, response_body);
        if(write_stat != AXIS2_SUCCESS)
        {
            AXIS2_HANDLE_ERROR(env, AXIS2_ERROR_WRITING_RESPONSE, AXIS2_FAILURE);
            axis2_http_response_writer_free(response_writer, env);
            return AXIS2_FAILURE;
        }
    }
    else if(!binary_content)
    {
        /* Sending a normal SOAP response enabling http chunking */
        axutil_http_chunked_stream_t *chunked_stream = NULL;
        int left = body_size;
        chunked_stream = axutil_http_chunked_stream_create(env, svr_conn->stream);
        while(left > 0)
        {
            int len = -1;
            len = axutil_http_chunked_stream_write(chunked_stream, env, response_body, body_size);
            if(len <= 0)
            {
                AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "cannot write data to chunked stream");
                axutil_http_chunked_stream_free(chunked_stream, env);
                axis2_http_response_writer_free(response_writer, env);
                return AXIS2_FAILURE;
            }
            left -= len;
        }
        axutil_http_chunked_stream_write_last_chunk(chunked_stream, env);
        axutil_http_chunked_stream_free(chunked_stream, env);
    }
    else
    {
        /* In the MTOM case we enable chunking in order to send the attachment */
        axutil_http_chunked_stream_t *chunked_stream = NULL;
        axis2_status_t write_stat = AXIS2_FAILURE;
        axutil_array_list_t *mime_parts = NULL;
        axis2_char_t *mtom_sending_callback_name = NULL;

        mime_parts = axis2_http_simple_response_get_mime_parts(response, env);
        if(!mime_parts)
        {
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "No mime parts are given");
            axis2_http_response_writer_free(response_writer, env);
            return AXIS2_FAILURE;
        }

        /* If the callback name is not there, then we will check whether there 
         * is any mime_parts which has type callback. If we found then no point 
         * of continuing we should return a failure */
        mtom_sending_callback_name = axis2_http_simple_response_get_mtom_sending_callback_name(
            response, env);
        if(!mtom_sending_callback_name)
        {
            if(axis2_http_transport_utils_is_callback_required(env, mime_parts))
            {
                AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "Sender callback not specified");
                axis2_http_response_writer_free(response_writer, env);
                return AXIS2_FAILURE;
            }
        }

        chunked_stream = axutil_http_chunked_stream_create(env, svr_conn->stream);
        write_stat = axis2_http_transport_utils_send_mtom_message(chunked_stream, env, mime_parts,
            mtom_sending_callback_name);
        axutil_http_chunked_stream_free(chunked_stream, env);

        if(write_stat != AXIS2_SUCCESS)
        {
            AXIS2_LOG_ERROR(env->log, AXIS2_LOG_SI, "writing mime parts failed");
            axis2_http_response_writer_free(response_writer, env);
            return AXIS2_FAILURE;
        }
    }

    axis2_http_response_writer_free(response_writer, env);
    return AXIS2_SUCCESS;
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_simple_http_svr_conn_set_rcv_timeout(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env,
    int timeout)
{
    return axutil_network_handler_set_sock_option(env, svr_conn->socket, SO_RCVTIMEO, timeout);
}

AXIS2_EXTERN axis2_status_t AXIS2_CALL
axis2_simple_http_svr_conn_set_snd_timeout(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env,
    int timeout)
{
    return axutil_network_handler_set_sock_option(env, svr_conn->socket, SO_SNDTIMEO, timeout);
}

AXIS2_EXTERN axis2_char_t *AXIS2_CALL
axis2_simple_http_svr_conn_get_svr_ip(
    const axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    return axutil_network_handler_get_svr_ip(env, svr_conn->socket);
}

AXIS2_EXTERN axis2_char_t *AXIS2_CALL
axis2_simple_http_svr_conn_get_peer_ip(
    const axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    return axutil_network_handler_get_peer_ip(env, svr_conn->socket);
}

static axis2_char_t *
axis2_simple_http_svr_conn_read_line(
    axis2_simple_http_svr_conn_t * svr_conn,
    const axutil_env_t * env)
{
    axis2_char_t* str_line = NULL;
    axis2_char_t tmp_buf[2048];
    int read = -1;

    /* peek for 2047 characters to verify whether it contains CRLF character */
    while((read = axutil_stream_peek(svr_conn->stream, env, tmp_buf, 2048 - 1)) > 0)
    {
        axis2_char_t *start = tmp_buf;
        axis2_char_t *end = NULL;
        tmp_buf[read] = AXIS2_ESC_NULL;
        end = strstr(tmp_buf, AXIS2_HTTP_CRLF);
        if(end)
        {
            axis2_char_t *buffer = NULL;
            if(str_line)
            {
                /* header is more than 2048 character. this is not a common case, and not optimized
                 * for performance (reading in a temp buffer and then strcat to get final buffer */
                buffer = tmp_buf;
            }
            else
            {
                /* header is less than 2048 characters, this is the common case. So to improve
                 * the performance, the buffer is malloc and then used to read the stream. */
                buffer = (axis2_char_t *)AXIS2_MALLOC(env->allocator, end - start + 3);
            }

            /* read the data including CRLF (hence the size = end - start + 2) */
            read = axutil_stream_read(svr_conn->stream, env, buffer, end - start + 2);
            if(read > 0)
            {
                buffer[read] = AXIS2_ESC_NULL;

                if(str_line)
                {
                    axis2_char_t* tmp_str_line = NULL;
                    tmp_str_line = axutil_stracat(env, str_line, buffer);
                    if(tmp_str_line)
                    {
                        AXIS2_FREE(env->allocator, str_line);
                        str_line = tmp_str_line;
                    }
                }
                else
                {
                    str_line = buffer;
                }
            }
            else
            {
                /* read returns 0 or negative value, this could be an error */
                if(str_line)
                {
                    AXIS2_FREE(env->allocator, str_line);
                    str_line = NULL;
                }
                else
                {
                    AXIS2_FREE(env->allocator, buffer);
                }
            }
            break;
        }
        else
        {
            /* not reached end yet */
            read = axutil_stream_read(svr_conn->stream, env, tmp_buf, 2048 - 1);
            if(read > 0)
            {
                axis2_char_t* tmp_str_line = NULL;
                tmp_buf[read] = AXIS2_ESC_NULL;
                tmp_str_line = axutil_stracat(env, str_line, tmp_buf);
                if(tmp_str_line)
                {
                    if(str_line)
                    {
                        AXIS2_FREE(env->allocator, str_line);
                    }
                    str_line = tmp_str_line;
                }
            }
        }
    }

    return str_line;
}