| 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 |
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 | #include <macros.h>
|
|---|
| 41 |
|
|---|
| 42 | #include <inet/endpoint.h>
|
|---|
| 43 | #include <inet/host.h>
|
|---|
| 44 | #include <inet/tcp.h>
|
|---|
| 45 |
|
|---|
| 46 | #include <http/http.h>
|
|---|
| 47 | #include <http/receive-buffer.h>
|
|---|
| 48 |
|
|---|
| 49 | static ssize_t http_receive(void *client_data, void *buf, size_t buf_size)
|
|---|
| 50 | {
|
|---|
| 51 | http_t *http = client_data;
|
|---|
| 52 | size_t nrecv;
|
|---|
| 53 | int rc;
|
|---|
| 54 |
|
|---|
| 55 | rc = tcp_conn_recv_wait(http->conn, buf, buf_size, &nrecv);
|
|---|
| 56 | if (rc != EOK)
|
|---|
| 57 | return rc;
|
|---|
| 58 |
|
|---|
| 59 | return nrecv;
|
|---|
| 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;
|
|---|
| 76 | int rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
|
|---|
| 77 | http_receive, http);
|
|---|
| 78 | if (rc != EOK) {
|
|---|
| 79 | free(http);
|
|---|
| 80 | return NULL;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | return http;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | int http_connect(http_t *http)
|
|---|
| 87 | {
|
|---|
| 88 | if (http->conn != NULL)
|
|---|
| 89 | return EBUSY;
|
|---|
| 90 |
|
|---|
| 91 | int rc = inet_host_plookup_one(http->host, ip_any, &http->addr, NULL,
|
|---|
| 92 | NULL);
|
|---|
| 93 | if (rc != EOK)
|
|---|
| 94 | return rc;
|
|---|
| 95 |
|
|---|
| 96 | inet_ep2_t epp;
|
|---|
| 97 |
|
|---|
| 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;
|
|---|
| 105 |
|
|---|
| 106 | rc = tcp_conn_create(http->tcp, &epp, NULL, NULL, &http->conn);
|
|---|
| 107 | if (rc != EOK)
|
|---|
| 108 | return rc;
|
|---|
| 109 |
|
|---|
| 110 | rc = tcp_conn_wait_connected(http->conn);
|
|---|
| 111 | if (rc != EOK)
|
|---|
| 112 | return rc;
|
|---|
| 113 |
|
|---|
| 114 | return rc;
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | int http_close(http_t *http)
|
|---|
| 118 | {
|
|---|
| 119 | if (http->conn == NULL)
|
|---|
| 120 | return EINVAL;
|
|---|
| 121 |
|
|---|
| 122 | tcp_conn_destroy(http->conn);
|
|---|
| 123 | tcp_destroy(http->tcp);
|
|---|
| 124 | return EOK;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | void http_destroy(http_t *http)
|
|---|
| 128 | {
|
|---|
| 129 | recv_buffer_fini(&http->recv_buffer);
|
|---|
| 130 | free(http);
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | /** @}
|
|---|
| 134 | */
|
|---|