source: mainline/uspace/app/download/main.c@ 3ce68b7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 3ce68b7 was c17469e, checked in by Martin Sucha <sucha14@…>, 12 years ago

download: Fix double free

  • Property mode set to 100644
File size: 5.3 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 download
30 * @{
31 */
32
33/** @file
34 * Download a file from a HTTP server
35 *
36 */
37
38#include <stdio.h>
39#include <stdlib.h>
40#include <str.h>
41#include <str_error.h>
42#include <task.h>
43#include <macros.h>
44
45#include <net/in.h>
46#include <net/inet.h>
47#include <net/socket.h>
48
49#include <http.h>
50#include <uri.h>
51
52#define NAME "download"
53#ifdef TIMESTAMP_UNIX
54#define VERSION STRING(RELEASE) "-" STRING(TIMESTAMP_UNIX)
55#else
56#define VERSION STRING(RELEASE)
57#endif
58#define USER_AGENT "HelenOS-" NAME "/" VERSION
59
60static void syntax_print(void)
61{
62 fprintf(stderr, "Usage: download <url>\n");
63 fprintf(stderr, " This will write the data to stdout, so you may want\n");
64 fprintf(stderr, " to redirect the output, e.g.\n");
65 fprintf(stderr, "\n");
66 fprintf(stderr, " download http://helenos.org/ | to helenos.html\n\n");
67}
68
69int main(int argc, char *argv[])
70{
71 if (argc != 2) {
72 syntax_print();
73 return 2;
74 }
75
76 uri_t *uri = uri_parse(argv[1]);
77 if (uri == NULL) {
78 fprintf(stderr, "Failed parsing URI\n");
79 return 2;
80 }
81
82 if (!uri_validate(uri)) {
83 fprintf(stderr, "The URI is invalid\n");
84 return 2;
85 }
86
87 /* TODO uri_normalize(uri) */
88
89 if (str_cmp(uri->scheme, "http") != 0) {
90 fprintf(stderr, "Only http scheme is supported at the moment\n");
91 return 2;
92 }
93
94 if (uri->host == NULL) {
95 fprintf(stderr, "host not set\n");
96 return 2;
97 }
98
99 uint16_t port = 80;
100 if (uri->port != NULL) {
101 int rc = str_uint16_t(uri->port, NULL, 10, true, &port);
102 if (rc != EOK) {
103 fprintf(stderr, "Invalid port number: %s\n", uri->port);
104 return 2;
105 }
106 }
107
108 const char *path = uri->path;
109 if (path == NULL || *path == 0)
110 path = "/";
111 char *server_path = NULL;
112 if (uri->query == NULL) {
113 server_path = str_dup(path);
114 if (server_path == NULL) {
115 fprintf(stderr, "Failed allocating path\n");
116 uri_destroy(uri);
117 return 3;
118 }
119 }
120 else {
121 int rc = asprintf(&server_path, "%s?%s", path, uri->query);
122 if (rc < 0) {
123 fprintf(stderr, "Failed allocating path\n");
124 uri_destroy(uri);
125 return 3;
126 }
127 }
128
129 http_request_t *req = http_request_create("GET", server_path);
130 free(server_path);
131 if (req == NULL) {
132 fprintf(stderr, "Failed creating request\n");
133 uri_destroy(uri);
134 return 3;
135 }
136
137 http_header_t *header_host = http_header_create("Host", uri->host);
138 if (header_host == NULL) {
139 fprintf(stderr, "Failed creating Host header\n");
140 uri_destroy(uri);
141 return 3;
142 }
143 list_append(&header_host->link, &req->headers);
144
145 http_header_t *header_ua = http_header_create("User-Agent", USER_AGENT);
146 if (header_ua == NULL) {
147 fprintf(stderr, "Failed creating User-Agent header\n");
148 uri_destroy(uri);
149 return 3;
150 }
151 list_append(&header_ua->link, &req->headers);
152
153 http_t *http = http_create(uri->host, port);
154 if (http == NULL) {
155 uri_destroy(uri);
156 fprintf(stderr, "Failed creating HTTP object\n");
157 return 3;
158 }
159
160 int rc = http_connect(http);
161 if (rc != EOK) {
162 fprintf(stderr, "Failed connecting: %s\n", str_error(rc));
163 uri_destroy(uri);
164 return rc;
165 }
166
167 rc = http_send_request(http, req);
168 if (rc != EOK) {
169 fprintf(stderr, "Failed sending request: %s\n", str_error(rc));
170 uri_destroy(uri);
171 return rc;
172 }
173
174 http_response_t *response = NULL;
175 rc = http_receive_response(http, &response);
176 if (rc != EOK) {
177 fprintf(stderr, "Failed receiving response: %s\n", str_error(rc));
178 uri_destroy(uri);
179 return rc;
180 }
181
182 if (response->status != 200) {
183 fprintf(stderr, "Server returned status %d %s\n", response->status,
184 response->message);
185 }
186 else {
187 size_t buf_size = 4096;
188 void *buf = malloc(buf_size);
189 if (buf == NULL) {
190 fprintf(stderr, "Failed allocating buffer\n)");
191 uri_destroy(uri);
192 return ENOMEM;
193 }
194
195 int body_size;
196 while ((body_size = http_receive_body(http, buf, buf_size)) > 0) {
197 fwrite(buf, 1, body_size, stdout);
198 }
199
200 if (body_size != 0) {
201 fprintf(stderr, "Failed receiving body: %s", str_error(body_size));
202 }
203 }
204
205 uri_destroy(uri);
206 return EOK;
207}
208
209/** @}
210 */
Note: See TracBrowser for help on using the repository browser.