| 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 <stdio.h>
|
|---|
| 37 | #include <stdlib.h>
|
|---|
| 38 | #include <str.h>
|
|---|
| 39 | #include <macros.h>
|
|---|
| 40 |
|
|---|
| 41 | #include <net/socket.h>
|
|---|
| 42 | #include <inet/dnsr.h>
|
|---|
| 43 |
|
|---|
| 44 | #include <http/http.h>
|
|---|
| 45 | #include <http/receive-buffer.h>
|
|---|
| 46 |
|
|---|
| 47 | static ssize_t http_receive(void *client_data, void *buf, size_t buf_size)
|
|---|
| 48 | {
|
|---|
| 49 | http_t *http = client_data;
|
|---|
| 50 | return recv(http->conn_sd, buf, buf_size, 0);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | http_t *http_create(const char *host, uint16_t port)
|
|---|
| 54 | {
|
|---|
| 55 | http_t *http = malloc(sizeof(http_t));
|
|---|
| 56 | if (http == NULL)
|
|---|
| 57 | return NULL;
|
|---|
| 58 |
|
|---|
| 59 | http->host = str_dup(host);
|
|---|
| 60 | if (http->host == NULL) {
|
|---|
| 61 | free(http);
|
|---|
| 62 | return NULL;
|
|---|
| 63 | }
|
|---|
| 64 | http->port = port;
|
|---|
| 65 |
|
|---|
| 66 | http->buffer_size = 4096;
|
|---|
| 67 | int rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
|
|---|
| 68 | http_receive, http);
|
|---|
| 69 | if (rc != EOK) {
|
|---|
| 70 | free(http);
|
|---|
| 71 | return NULL;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | return http;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | int http_connect(http_t *http)
|
|---|
| 78 | {
|
|---|
| 79 | if (http->connected)
|
|---|
| 80 | return EBUSY;
|
|---|
| 81 |
|
|---|
| 82 | /* Interpret as address */
|
|---|
| 83 | int rc = inet_addr_parse(http->host, &http->addr);
|
|---|
| 84 |
|
|---|
| 85 | if (rc != EOK) {
|
|---|
| 86 | /* Interpret as a host name */
|
|---|
| 87 | dnsr_hostinfo_t *hinfo = NULL;
|
|---|
| 88 | rc = dnsr_name2host(http->host, &hinfo, ip_any);
|
|---|
| 89 |
|
|---|
| 90 | if (rc != EOK)
|
|---|
| 91 | return rc;
|
|---|
| 92 |
|
|---|
| 93 | http->addr = hinfo->addr;
|
|---|
| 94 | dnsr_hostinfo_destroy(hinfo);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | struct sockaddr *saddr;
|
|---|
| 98 | socklen_t saddrlen;
|
|---|
| 99 |
|
|---|
| 100 | rc = inet_addr_sockaddr(&http->addr, http->port, &saddr, &saddrlen);
|
|---|
| 101 | if (rc != EOK) {
|
|---|
| 102 | assert(rc == ENOMEM);
|
|---|
| 103 | return ENOMEM;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | http->conn_sd = socket(saddr->sa_family, SOCK_STREAM, 0);
|
|---|
| 107 | if (http->conn_sd < 0)
|
|---|
| 108 | return http->conn_sd;
|
|---|
| 109 |
|
|---|
| 110 | rc = connect(http->conn_sd, saddr, saddrlen);
|
|---|
| 111 | free(saddr);
|
|---|
| 112 |
|
|---|
| 113 | return rc;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | int http_close(http_t *http)
|
|---|
| 117 | {
|
|---|
| 118 | if (!http->connected)
|
|---|
| 119 | return EINVAL;
|
|---|
| 120 |
|
|---|
| 121 | return closesocket(http->conn_sd);
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | void http_destroy(http_t *http)
|
|---|
| 125 | {
|
|---|
| 126 | recv_buffer_fini(&http->recv_buffer);
|
|---|
| 127 | free(http);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** @}
|
|---|
| 131 | */
|
|---|