File: http_post_test.c

package info (click to toggle)
globus-xio 6.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 8,128 kB
  • sloc: ansic: 46,711; sh: 11,139; perl: 1,598; makefile: 289
file content (777 lines) | stat: -rw-r--r-- 20,545 bytes parent folder | download | duplicates (5)
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
/*
 * Copyright 1999-2006 University of Chicago
 * 
 * Licensed 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.
 */

/**
 * @file http_post_test.c
 * @brief HTTP Echo Test
 *
 * Test that clients can send and receive HTTP message bodies.
 *
 * Test parameters are
 * - -f filename<br>
 *   Name of file to be used as the test message
 * - -v "HTTP/1.0"|"HTTP/1.1"<br>
 *   Set HTTP version to use
 * - -t "chunked"|"identity"<br>
 *   Set transfer encoding (for HTTP/1.1 transfers only)
 * - -b buffer-size<br>
 *   Set the size (in bytes) to be read/written at a time
 * - -i iterations
 *   Set the number of transfer iterations to do
 *
 * The test client will send the POST request for the /post-test URI.
 *
 * The test server will 
 * - verify the receipt of the /post-test URI
 * - compare the message body to that of the test file.
 * - generate a response which contains the test file as the body.
 *
 * Test test client will then compare the response message with the contents
 * of the file.
 */
#include "globus_common.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "globus_xio.h"
#include "globus_xio_http.h"
#include "globus_xio_tcp_driver.h"

#include "http_test_common.h"

#ifdef _WIN32
#include <getopt.h>
#endif

int                                     done = 0;
globus_byte_t *                         message_body;
long                                    file_size;
globus_xio_http_version_t               version = GLOBUS_XIO_HTTP_VERSION_UNSET;
size_t                                  buffer_size = 0;
char *                                  transfer_encoding = NULL;
globus_xio_driver_t                     tcp_driver;
globus_xio_driver_t                     http_driver;
globus_xio_stack_t                      stack;
int                                     iterations = 1;

#define HTTP_1_0 "HTTP/1.0"
#define HTTP_1_1 "HTTP/1.1"
#define CHUNKED  "chunked"
#define IDENTITY "identity"


static
void
globus_l_xio_test_server_request_callback(
    void *                              user_arg,
    globus_result_t                     result,
    const char *                        method,
    const char *                        uri,
    globus_xio_http_version_t           http_version,
    globus_hashtable_t                  headers);

static
int
globus_l_xio_test_read_file(
    const char *                        filename);

static
globus_result_t
globus_l_xio_test_write_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     message,
    globus_size_t                       message_size,
    globus_size_t                       buffer_size);

static
globus_result_t
globus_l_xio_test_read_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     message,
    globus_size_t                       message_size,
    globus_size_t                       buffer_size);

void
globus_l_xio_test_server_handle_request(
    void *                              ignored);

static void usage(const char * cmd)
{
    printf("Usage: %s [options] [-c|-s] -f filename\n"
            "Options:\n"
            "    -v \"HTTP/1.0\" | \"HTTP/1.1\"    HTTP version\n"
            "    -t \"chunked\" | \"identity\"     Client Transfer-Encoding\n"
            "    -i iterations                     Set number of iterations\n"
            "    -b buffer-size                    Size of reads and writes\n",
            cmd);
}

int
client_main(
    const char *                        filename,
    const char *                        contact,
    globus_xio_http_version_t           http_version)
{
    int                                 rc;
    globus_result_t                     result;
    int                                 header_cnt = 0;
    char                                content_length_buffer[64];
    globus_xio_http_header_t            headers[2];
    globus_xio_handle_t                 handle;
    int                                 i;
    globus_xio_data_descriptor_t        descriptor;
    globus_byte_t                       buffer[1];
    int                                 status_code;
    char *                              reason_phrase;

    rc = globus_l_xio_test_read_file(filename);
    if (rc != 0)
    {
        goto error_exit;
    }

    if (transfer_encoding != NULL)
    {
        headers[header_cnt].name = "Transfer-Encoding";
        headers[header_cnt].value = transfer_encoding;

        header_cnt++;

    }

    if ((http_version == GLOBUS_XIO_HTTP_VERSION_1_0) ||
            ((transfer_encoding != NULL)
                && strcmp(transfer_encoding, IDENTITY) == 0))
    {
        sprintf(content_length_buffer, "%ld", file_size);

        headers[header_cnt].name = "Content-Length";
        headers[header_cnt].value = &content_length_buffer[0];

        header_cnt++;
    }

    handle = NULL;
    for (i = 0; i < iterations; i++)
    {

        if (handle != NULL)
        {
            globus_xio_close(handle, NULL);
        }
        result = http_test_client_request(
                &handle,
                tcp_driver,
                http_driver,
                stack,
                contact,
                "%2fpost-test",
                "POST",
                http_version,
                headers,
                header_cnt);

        if (result != GLOBUS_SUCCESS)
        {
            fprintf(stderr, "Error making request: %s\n",
                    globus_object_printable_to_string(
                    globus_error_get(result)));
            rc = 50;
            goto error_exit;
        }

        result = globus_l_xio_test_write_buffer(
                handle,
                message_body,
                file_size,
                buffer_size);

        if (result != GLOBUS_SUCCESS)
        {
            rc = 51;
            break;
        }

        /* READ RESPONSE */
        result = globus_xio_data_descriptor_init(&descriptor, handle);
        if (result != GLOBUS_SUCCESS)
        {
            rc = 51;
            goto close_exit;
        }
        result = globus_xio_read(
                handle,
                buffer,
                0,
                0,
                NULL,
                descriptor);
        if (result != GLOBUS_SUCCESS)
        {
            rc = 51;
            goto close_exit;
        }
        result = globus_xio_data_descriptor_cntl(
                descriptor,
                http_driver,
                GLOBUS_XIO_HTTP_GET_RESPONSE,
                &status_code,
                &reason_phrase,
                NULL,
                NULL);
        if (result != GLOBUS_SUCCESS || status_code < 200 || status_code > 299)
        {
            fprintf(stderr, "Get failed with \"%03d %s\"\n",
                    status_code,
                    reason_phrase);
            rc = 51;
            goto close_exit;
        }

        result = globus_l_xio_test_read_buffer(
                handle,
                message_body,
                file_size,
                buffer_size);

        if (result != GLOBUS_SUCCESS)
        {
            rc = 52;
        }

        if (rc != 0)
        {
            break;
        }
    }

close_exit:
    globus_xio_close(handle, NULL);
error_exit:
    if (rc == 0)
    {
        printf("Success\n");
    }
    else
    {
        printf("Error\n");
    }

    return rc;
}
/* client_main() */


static
int
server_main(
    const char *                        filename)
{
    int                                 rc;
    globus_result_t                     result;
    http_test_server_t                  test_server;

    rc = globus_l_xio_test_read_file(filename);

    if (rc != 0)
    {
        goto error_exit;
    }

    result = http_test_server_init(
            &test_server,
            tcp_driver,
            http_driver,
            stack);

    if (result != GLOBUS_SUCCESS)
    {
        rc = 29;

        goto error_exit;
    }

    result = http_test_server_register_handler(
            &test_server,
            "/post-test",
            globus_l_xio_test_server_request_callback,
            &test_server);

    printf("%s\n", test_server.contact);

    fflush(stdout);

    result = http_test_server_run(&test_server);

    if (result != GLOBUS_SUCCESS)
    {
        rc = 30;

        goto error_exit;
    }
    http_test_server_destroy(&test_server);

error_exit:
    return rc;
}
/* server_main() */

static
void
globus_l_xio_test_server_request_callback(
    void *                              user_arg,
    globus_result_t                     result,
    const char *                        method,
    const char *                        uri,
    globus_xio_http_version_t           http_version,
    globus_hashtable_t                  headers)
{
    http_test_server_t *                test_server = user_arg;
    globus_xio_http_header_t            response_headers[2];
    globus_size_t                       header_cnt=0;
    char                                content_length_buffer[64];
    int                                 rc=0;

    if (result == GLOBUS_SUCCESS &&
            method != NULL && uri != NULL &&
            (strcmp(method, "POST") == 0) &&
            (strcmp(uri, "/post-test") == 0))
    {
        result = globus_l_xio_test_read_buffer(
            test_server->handle,
            message_body,
            file_size,
            buffer_size);

        if (result != GLOBUS_SUCCESS)
        {
            fprintf(stderr, "Error reading buffer: %s\n",
                    globus_object_printable_to_string(
                        globus_error_peek(result)));
            rc = 404;
            goto error_respond_exit;
        }

        if (transfer_encoding != NULL)
        {
            response_headers[header_cnt].name = "Transfer-Encoding";
            response_headers[header_cnt].value = transfer_encoding;

            header_cnt++;
        }

        if ((http_version == GLOBUS_XIO_HTTP_VERSION_1_0) ||
                ((transfer_encoding != NULL)
                    && strcmp(transfer_encoding, IDENTITY) == 0))
        {
                sprintf(content_length_buffer, "%ld", file_size);

                response_headers[header_cnt].name = "Content-Length";
                response_headers[header_cnt].value = &content_length_buffer[0];

                header_cnt++;
        }

        result = http_test_server_respond(
                test_server,
                rc,
                NULL,
                response_headers,
                header_cnt);

        if (result != GLOBUS_SUCCESS)
        {
            goto error_exit;
        }
    }
    else
    {
        rc = 404;

        goto error_respond_exit;
    }

    result = globus_l_xio_test_write_buffer(
            test_server->handle,
            message_body,
            (globus_size_t) file_size,
            buffer_size);

    if (result != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error writing buffer: %s\n",
                globus_object_printable_to_string(globus_error_peek(result)));
        goto error_exit;
    }

    if (--iterations == 0)
    {
        http_test_server_shutdown(test_server);
        http_test_server_close_handle(test_server);
    }

    return;

error_respond_exit:
    http_test_server_respond(
            test_server,
            rc,
            NULL,
            NULL,
            0);

error_exit:
    http_test_server_shutdown(test_server);
    http_test_server_close_handle(test_server);

}
/* globus_l_xio_test_server_request_callback() */


static
int
globus_l_xio_test_read_file(
    const char *                        filename)
{
    int                                 rc;
    FILE *                              fp;

    fp = fopen(filename, "rb");

    if (fp == NULL)
    {
        rc = 35;
        fprintf(stderr, "Error opening %s: %s\n",
                filename, strerror(errno));

        goto error_exit;
    }

    rc = fseek(fp, 0, SEEK_END);

    if (rc < 0)
    {
        rc = 36;
        fprintf(stderr, "Error seeking %s: %s\n",
                filename, strerror(errno));

        goto fclose_exit;
    }

    file_size = ftell(fp);
    if (file_size < 0)
    {
        rc = 37;
        fprintf(stderr, "Error getting file size: %s\n",
                strerror(errno));

        goto fclose_exit;
    }
    rewind(fp);

    message_body = malloc(file_size+1);
    if (message_body == NULL)
    {
        rc = 38;
        fprintf(stderr, "Error allocating buffer: %s\n",
                strerror(errno));

        goto fclose_exit;
    }
    rc = fread(message_body, (size_t) file_size, 1, fp);

    if (rc != 1)
    {
        rc = 39;
        fprintf(stderr, "Error reading %s: %s\n",
                filename, strerror(errno));
        goto fclose_exit;
    }
    message_body[(size_t)file_size] = '\0';

    rc = 0;

fclose_exit:
    fclose(fp);
error_exit:
    return rc;
}
/* globus_l_xio_test_read_file() */

static
globus_result_t
globus_l_xio_test_write_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     message,
    globus_size_t                       message_size,
    globus_size_t                       buffer_size)
{
    globus_byte_t *                     ptr = message;
    globus_size_t                       left = message_size;
    globus_size_t                       to_write;
    globus_size_t                       nbytes;
    globus_result_t                     result = GLOBUS_SUCCESS;
    GlobusXIOName(globus_l_xio_test_write_buffer);

    if (buffer_size == 0)
    {
        buffer_size = 2000;
    }
    while ((left > 0) && (result == GLOBUS_SUCCESS))
    {
        to_write = (left > buffer_size) ? buffer_size : left;
        result = globus_xio_write(
                handle,
                ptr,
                to_write,
                to_write,
                &nbytes,
                NULL);

        if (result == GLOBUS_SUCCESS)
        {
            if (nbytes != to_write)
            {
                fprintf(stderr, "Didn't write all I expected.\n");
                result = GlobusXIOErrorEOF();
            }
            left -= nbytes;
            ptr += nbytes;
        }
        else
        {
            fprintf(stderr, "Error writing data: %s\n",
                    globus_object_printable_to_string(globus_error_peek(result)));
        }
    }

    globus_xio_handle_cntl(
            handle,
            http_driver,
            GLOBUS_XIO_HTTP_HANDLE_SET_END_OF_ENTITY);

    return result;
}
/* globus_l_xio_test_write_buffer() */

static
globus_result_t
globus_l_xio_test_read_buffer(
    globus_xio_handle_t                 handle,
    globus_byte_t *                     message,
    globus_size_t                       message_size,
    globus_size_t                       buffer_size)
{
    globus_size_t                       offset=0;
    globus_size_t                       left = message_size;
    globus_size_t                       to_read;
    globus_size_t                       nbytes;
    globus_result_t                     result = GLOBUS_SUCCESS;
    globus_byte_t *                     buffer;
    GlobusXIOName(globus_l_xio_test_read_buffer);

    if (buffer_size == 0)
    {
        buffer_size = 2000;
    }

    buffer = malloc(buffer_size);

    if (buffer == NULL)
    {
        result = GlobusXIOErrorMemory("buffer");
    }
    while ((left > 0) || (result == GLOBUS_SUCCESS))
    {
        nbytes = 0;
        to_read = (left > buffer_size) ? buffer_size : 
                (left > 0 ? left : buffer_size);
        result = globus_xio_read(
                handle,
                buffer,
                buffer_size,
                1,
                &nbytes,
                NULL);

        if (nbytes > 0)
        {
            if (left > 0)
            {
                if (memcmp(message+offset, buffer, nbytes) != 0)
                {
                    fprintf(stderr, "File doesn't match\n");
                    result = GlobusXIOErrorParameter("buffer");
                }

                left -= nbytes;
            }
            else
            {
                fprintf(stderr, "File doesn't match\n");
                result = GlobusXIOErrorParameter("buffer");
            }
            offset += nbytes;
        }
    }
    if (offset == message_size && http_is_eof(result))
    {
        result = GLOBUS_SUCCESS;
    }
    else if (result != GLOBUS_SUCCESS)
    {
        fprintf(stderr, "Error reading from http: %s\n",
                globus_object_printable_to_string(globus_error_peek(result)));

        fprintf(stderr, "after reading %lu of %lu bytes\n",
                (unsigned long) offset, (unsigned long) message_size);
    }

    return result;
}
/* globus_l_xio_test_read_buffer() */

void
globus_l_xio_test_server_handle_request(
    void *                              ignored)
{
}
/* globus_l_xio_test_server_handle_request() */

int
main(
    int                                 argc,
    char *                              argv[])
{
    int                                 rc;
    char *                              filename = NULL;
    char *                              contact = NULL;
    globus_bool_t                       server = GLOBUS_FALSE;
    char                                gets_buffer[1024];

    while ((rc = getopt(argc, argv, "hf:cst:b:v:i:")) != EOF)
    {
        switch (rc)
        {
            case 'h':
                usage(argv[0]);
                exit(0);
            case 'f':
                filename = optarg;
                break;
            case 'c':
                server = GLOBUS_FALSE;
                contact = fgets(gets_buffer, sizeof(gets_buffer), stdin);
                break;
            case 's':
                server = GLOBUS_TRUE;
                contact = NULL;
                break;
            case 't':
                if ((strcmp(optarg, CHUNKED) == 0)
                        || (strcmp(optarg, IDENTITY) == 0))
                {
                    transfer_encoding = optarg;
                }
                else
                {
                    usage(argv[0]);
                    exit(1);
                }
                break;
            case 'b':
                rc = sscanf(optarg, "%zd", &buffer_size);
                if (rc != 1)
                {
                    usage(argv[0]);
                    exit(1);
                }
                break;
            case 'v':
                if (strcmp(optarg, HTTP_1_0) == 0)
                {
                    version = GLOBUS_XIO_HTTP_VERSION_1_0;
                }
                else if (strcmp(optarg, HTTP_1_1) == 0)
                {
                    version = GLOBUS_XIO_HTTP_VERSION_1_1;
                }
                else
                {
                    usage(argv[0]);
                    exit(1);
                }
                break;
            case 'i':
                iterations = atoi(optarg);

                if (iterations <= 0)
                {
                    usage(argv[0]);
                    exit(1);
                }
                break;
            default:
                usage(argv[0]);
                exit(1);
        }
    }
    if (((!server) && (contact == NULL)) || (filename == NULL))
    {
        usage(argv[0]);
        exit(1);
    }

    if (iterations > 1 && version == GLOBUS_XIO_HTTP_VERSION_1_0)
    {
        fprintf(stderr,
                "Can't have multiple iterations with HTTP/1.0 server\n");
        usage(argv[0]);
        exit(1);
    }

    rc = http_test_initialize(&tcp_driver, &http_driver, &stack);

    if (rc != 0)
    {
        goto error_exit;
    }

    if (server)
    {
        rc = server_main(
                filename);
    }
    else
    {
        rc = client_main(
                filename,
                contact,
                version);
    }

    globus_xio_stack_destroy(stack);
    globus_xio_driver_unload(http_driver);
    globus_xio_driver_unload(tcp_driver);

    globus_module_deactivate_all();

error_exit:
    return rc;
}
/* main() */