[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 |
|
---|
[ff364f1] | 49 | static ssize_t http_receive(void *client_data, void *buf, size_t buf_size)
|
---|
[d7b7f5e] | 50 | {
|
---|
[ff364f1] | 51 | http_t *http = client_data;
|
---|
[fab2746] | 52 | size_t nrecv;
|
---|
| 53 | int rc;
|
---|
| 54 |
|
---|
[779541b] | 55 | rc = tcp_conn_recv_wait(http->conn, buf, buf_size, &nrecv);
|
---|
[fab2746] | 56 | if (rc != EOK)
|
---|
| 57 | return rc;
|
---|
| 58 |
|
---|
| 59 | return nrecv;
|
---|
[d7b7f5e] | 60 | }
|
---|
| 61 |
|
---|
| 62 | http_t *http_create(const char *host, uint16_t port)
|
---|
| 63 | {
|
---|
| 64 | http_t *http = malloc(sizeof(http_t));
|
---|
| 65 | if (http == NULL)
|
---|
| 66 | return NULL;
|
---|
| 67 |
|
---|
| 68 | http->host = str_dup(host);
|
---|
| 69 | if (http->host == NULL) {
|
---|
| 70 | free(http);
|
---|
| 71 | return NULL;
|
---|
| 72 | }
|
---|
| 73 | http->port = port;
|
---|
| 74 |
|
---|
| 75 | http->buffer_size = 4096;
|
---|
[ff364f1] | 76 | int rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
|
---|
| 77 | http_receive, http);
|
---|
| 78 | if (rc != EOK) {
|
---|
[d7b7f5e] | 79 | free(http);
|
---|
| 80 | return NULL;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return http;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | int http_connect(http_t *http)
|
---|
| 87 | {
|
---|
[fab2746] | 88 | if (http->conn != NULL)
|
---|
[d7b7f5e] | 89 | return EBUSY;
|
---|
| 90 |
|
---|
[a62ceaf] | 91 | int rc = inet_host_plookup_one(http->host, ip_any, &http->addr, NULL,
|
---|
| 92 | NULL);
|
---|
| 93 | if (rc != EOK)
|
---|
| 94 | return rc;
|
---|
[d7b7f5e] | 95 |
|
---|
[fab2746] | 96 | inet_ep2_t epp;
|
---|
[d7b7f5e] | 97 |
|
---|
[fab2746] | 98 | inet_ep2_init(&epp);
|
---|
| 99 | epp.remote.addr = http->addr;
|
---|
| 100 | epp.remote.port = http->port;
|
---|
| 101 |
|
---|
| 102 | rc = tcp_create(&http->tcp);
|
---|
| 103 | if (rc != EOK)
|
---|
| 104 | return rc;
|
---|
[c42f50d] | 105 |
|
---|
[fab2746] | 106 | rc = tcp_conn_create(http->tcp, &epp, NULL, NULL, &http->conn);
|
---|
| 107 | if (rc != EOK)
|
---|
| 108 | return rc;
|
---|
[d7b7f5e] | 109 |
|
---|
[fab2746] | 110 | rc = tcp_conn_wait_connected(http->conn);
|
---|
| 111 | if (rc != EOK)
|
---|
| 112 | return rc;
|
---|
[d7b7f5e] | 113 |
|
---|
| 114 | return rc;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | int http_close(http_t *http)
|
---|
| 118 | {
|
---|
[fab2746] | 119 | if (http->conn == NULL)
|
---|
[d7b7f5e] | 120 | return EINVAL;
|
---|
| 121 |
|
---|
[fab2746] | 122 | tcp_conn_destroy(http->conn);
|
---|
| 123 | tcp_destroy(http->tcp);
|
---|
| 124 | return EOK;
|
---|
[d7b7f5e] | 125 | }
|
---|
| 126 |
|
---|
| 127 | void http_destroy(http_t *http)
|
---|
| 128 | {
|
---|
[ff364f1] | 129 | recv_buffer_fini(&http->recv_buffer);
|
---|
[d7b7f5e] | 130 | free(http);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | /** @}
|
---|
| 134 | */
|
---|