[d7b7f5e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Martin Sucha
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup http
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[fab2746] | 36 | #include <errno.h>
|
---|
[d7b7f5e] | 37 | #include <stdio.h>
|
---|
| 38 | #include <stdlib.h>
|
---|
| 39 | #include <str.h>
|
---|
| 40 | #include <macros.h>
|
---|
| 41 |
|
---|
[a62ceaf] | 42 | #include <inet/endpoint.h>
|
---|
| 43 | #include <inet/host.h>
|
---|
[fab2746] | 44 | #include <inet/tcp.h>
|
---|
[d7b7f5e] | 45 |
|
---|
[b623b68] | 46 | #include <http/http.h>
|
---|
| 47 | #include <http/receive-buffer.h>
|
---|
[d7b7f5e] | 48 |
|
---|
[b7fd2a0] | 49 | static errno_t http_receive(void *client_data, void *buf, size_t buf_size,
|
---|
[9a09212] | 50 | size_t *nrecv)
|
---|
[d7b7f5e] | 51 | {
|
---|
[ff364f1] | 52 | http_t *http = client_data;
|
---|
[fab2746] | 53 |
|
---|
[9a09212] | 54 | return tcp_conn_recv_wait(http->conn, buf, buf_size, nrecv);
|
---|
[d7b7f5e] | 55 | }
|
---|
| 56 |
|
---|
| 57 | http_t *http_create(const char *host, uint16_t port)
|
---|
| 58 | {
|
---|
| 59 | http_t *http = malloc(sizeof(http_t));
|
---|
| 60 | if (http == NULL)
|
---|
| 61 | return NULL;
|
---|
[a35b458] | 62 |
|
---|
[d7b7f5e] | 63 | http->host = str_dup(host);
|
---|
| 64 | if (http->host == NULL) {
|
---|
| 65 | free(http);
|
---|
| 66 | return NULL;
|
---|
| 67 | }
|
---|
| 68 | http->port = port;
|
---|
[a35b458] | 69 |
|
---|
[d7b7f5e] | 70 | http->buffer_size = 4096;
|
---|
[b7fd2a0] | 71 | errno_t rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
|
---|
[ff364f1] | 72 | http_receive, http);
|
---|
| 73 | if (rc != EOK) {
|
---|
[d7b7f5e] | 74 | free(http);
|
---|
| 75 | return NULL;
|
---|
| 76 | }
|
---|
[a35b458] | 77 |
|
---|
[d7b7f5e] | 78 | return http;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[b7fd2a0] | 81 | errno_t http_connect(http_t *http)
|
---|
[d7b7f5e] | 82 | {
|
---|
[fab2746] | 83 | if (http->conn != NULL)
|
---|
[d7b7f5e] | 84 | return EBUSY;
|
---|
[a35b458] | 85 |
|
---|
[b7fd2a0] | 86 | errno_t rc = inet_host_plookup_one(http->host, ip_any, &http->addr, NULL,
|
---|
[a62ceaf] | 87 | NULL);
|
---|
| 88 | if (rc != EOK)
|
---|
| 89 | return rc;
|
---|
[a35b458] | 90 |
|
---|
[fab2746] | 91 | inet_ep2_t epp;
|
---|
[a35b458] | 92 |
|
---|
[fab2746] | 93 | inet_ep2_init(&epp);
|
---|
| 94 | epp.remote.addr = http->addr;
|
---|
| 95 | epp.remote.port = http->port;
|
---|
[a35b458] | 96 |
|
---|
[fab2746] | 97 | rc = tcp_create(&http->tcp);
|
---|
| 98 | if (rc != EOK)
|
---|
| 99 | return rc;
|
---|
[a35b458] | 100 |
|
---|
[fab2746] | 101 | rc = tcp_conn_create(http->tcp, &epp, NULL, NULL, &http->conn);
|
---|
| 102 | if (rc != EOK)
|
---|
| 103 | return rc;
|
---|
[a35b458] | 104 |
|
---|
[fab2746] | 105 | rc = tcp_conn_wait_connected(http->conn);
|
---|
| 106 | if (rc != EOK)
|
---|
| 107 | return rc;
|
---|
[a35b458] | 108 |
|
---|
[d7b7f5e] | 109 | return rc;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[b7fd2a0] | 112 | errno_t http_close(http_t *http)
|
---|
[d7b7f5e] | 113 | {
|
---|
[fab2746] | 114 | if (http->conn == NULL)
|
---|
[d7b7f5e] | 115 | return EINVAL;
|
---|
[a35b458] | 116 |
|
---|
[fab2746] | 117 | tcp_conn_destroy(http->conn);
|
---|
[fc3d4fd5] | 118 | http->conn = NULL;
|
---|
[fab2746] | 119 | tcp_destroy(http->tcp);
|
---|
[fc3d4fd5] | 120 | http->tcp = NULL;
|
---|
[a35b458] | 121 |
|
---|
[fab2746] | 122 | return EOK;
|
---|
[d7b7f5e] | 123 | }
|
---|
| 124 |
|
---|
| 125 | void http_destroy(http_t *http)
|
---|
| 126 | {
|
---|
[fc3d4fd5] | 127 | (void) http_close(http);
|
---|
[ff364f1] | 128 | recv_buffer_fini(&http->recv_buffer);
|
---|
[d7b7f5e] | 129 | free(http);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /** @}
|
---|
| 133 | */
|
---|