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