source: mainline/uspace/lib/http/src/http.c@ 3061bc1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3061bc1 was b7fd2a0, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use errno_t in all uspace and kernel code.

Change type of every variable, parameter and return value that holds an
<errno.h> constant to either errno_t (the usual case), or sys_errno_t
(some places in kernel). This is for the purpose of self-documentation,
as well as for type-checking with a bit of type definition hackery.

Although this is a massive commit, it is a simple text replacement, and thus
is very easy to verify. Simply do the following:

`
git checkout <this commit's hash>
git reset HEAD
git add .
tools/srepl '\berrno_t\b' int
git add .
tools/srepl '\bsys_errno_t\b' sysarg_t
git reset
git diff
`

While this doesn't ensure that the replacements are correct, it does ensure
that the commit doesn't do anything except those replacements. Since errno_t
is typedef'd to int in the usual case (and sys_errno_t to sysarg_t), even if
incorrect, this commit cannot change behavior.

  • Property mode set to 100644
File size: 3.2 KB
Line 
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
49static errno_t http_receive(void *client_data, void *buf, size_t buf_size,
50 size_t *nrecv)
51{
52 http_t *http = client_data;
53
54 return tcp_conn_recv_wait(http->conn, buf, buf_size, nrecv);
55}
56
57http_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;
62
63 http->host = str_dup(host);
64 if (http->host == NULL) {
65 free(http);
66 return NULL;
67 }
68 http->port = port;
69
70 http->buffer_size = 4096;
71 errno_t rc = recv_buffer_init(&http->recv_buffer, http->buffer_size,
72 http_receive, http);
73 if (rc != EOK) {
74 free(http);
75 return NULL;
76 }
77
78 return http;
79}
80
81errno_t http_connect(http_t *http)
82{
83 if (http->conn != NULL)
84 return EBUSY;
85
86 errno_t rc = inet_host_plookup_one(http->host, ip_any, &http->addr, NULL,
87 NULL);
88 if (rc != EOK)
89 return rc;
90
91 inet_ep2_t epp;
92
93 inet_ep2_init(&epp);
94 epp.remote.addr = http->addr;
95 epp.remote.port = http->port;
96
97 rc = tcp_create(&http->tcp);
98 if (rc != EOK)
99 return rc;
100
101 rc = tcp_conn_create(http->tcp, &epp, NULL, NULL, &http->conn);
102 if (rc != EOK)
103 return rc;
104
105 rc = tcp_conn_wait_connected(http->conn);
106 if (rc != EOK)
107 return rc;
108
109 return rc;
110}
111
112errno_t http_close(http_t *http)
113{
114 if (http->conn == NULL)
115 return EINVAL;
116
117 tcp_conn_destroy(http->conn);
118 http->conn = NULL;
119 tcp_destroy(http->tcp);
120 http->tcp = NULL;
121
122 return EOK;
123}
124
125void http_destroy(http_t *http)
126{
127 (void) http_close(http);
128 recv_buffer_fini(&http->recv_buffer);
129 free(http);
130}
131
132/** @}
133 */
Note: See TracBrowser for help on using the repository browser.