Changeset 9621c7d in mainline
- Timestamp:
- 2017-08-24T16:43:24Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- ac415d50
- Parents:
- 258d77e
- Files:
-
- 2 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
boot/Makefile.common
r258d77e r9621c7d 208 208 $(USPACE_PATH)/app/nterm/nterm \ 209 209 $(USPACE_PATH)/app/ping/ping \ 210 $(USPACE_PATH)/app/pkg/pkg \ 210 211 $(USPACE_PATH)/app/stats/stats \ 211 212 $(USPACE_PATH)/app/sysinfo/sysinfo \ -
uspace/Makefile
r258d77e r9621c7d 81 81 app/nic \ 82 82 app/ping \ 83 app/pkg \ 83 84 app/sysinfo \ 84 85 app/sysinst \ -
uspace/app/download/main.c
r258d77e r9621c7d 57 57 static void syntax_print(void) 58 58 { 59 fprintf(stderr, "Usage: download <url>\n");60 fprintf(stderr, " This will write the datato stdout, so you may want\n");59 fprintf(stderr, "Usage: download [-o <outfile>] <url>\n"); 60 fprintf(stderr, " Without -o, data will be written to stdout, so you may want\n"); 61 61 fprintf(stderr, " to redirect the output, e.g.\n"); 62 62 fprintf(stderr, "\n"); … … 66 66 int main(int argc, char *argv[]) 67 67 { 68 if (argc != 2) { 68 int i; 69 char *ofname = NULL; 70 FILE *ofile = NULL; 71 size_t buf_size = 4096; 72 void *buf = NULL; 73 uri_t *uri = NULL; 74 int rc; 75 76 if (argc < 2) { 69 77 syntax_print(); 70 return 2; 71 } 72 73 uri_t *uri = uri_parse(argv[1]); 78 rc = EINVAL; 79 goto error; 80 } 81 82 i = 1; 83 84 if (str_cmp(argv[i], "-o") == 0) { 85 ++i; 86 if (argc < i + 1) { 87 syntax_print(); 88 rc = EINVAL; 89 goto error; 90 } 91 92 ofname = argv[i++]; 93 ofile = fopen(ofname, "wb"); 94 if (ofile == NULL) { 95 fprintf(stderr, "Error creating '%s'.\n", ofname); 96 rc = EINVAL; 97 goto error; 98 } 99 } 100 101 if (argc != i + 1) { 102 syntax_print(); 103 rc = EINVAL; 104 goto error; 105 } 106 107 uri = uri_parse(argv[i]); 74 108 if (uri == NULL) { 75 109 fprintf(stderr, "Failed parsing URI\n"); 76 return 2; 110 rc = EINVAL; 111 goto error; 77 112 } 78 113 79 114 if (!uri_validate(uri)) { 80 115 fprintf(stderr, "The URI is invalid\n"); 81 return 2; 116 rc = EINVAL; 117 goto error; 82 118 } 83 119 … … 86 122 if (str_cmp(uri->scheme, "http") != 0) { 87 123 fprintf(stderr, "Only http scheme is supported at the moment\n"); 88 return 2; 124 rc = EINVAL; 125 goto error; 89 126 } 90 127 91 128 if (uri->host == NULL) { 92 129 fprintf(stderr, "host not set\n"); 93 return 2; 130 rc = EINVAL; 131 goto error; 94 132 } 95 133 96 134 uint16_t port = 80; 97 135 if (uri->port != NULL) { 98 intrc = str_uint16_t(uri->port, NULL, 10, true, &port);136 rc = str_uint16_t(uri->port, NULL, 10, true, &port); 99 137 if (rc != EOK) { 100 138 fprintf(stderr, "Invalid port number: %s\n", uri->port); 101 return 2; 139 rc = EINVAL; 140 goto error; 102 141 } 103 142 } … … 111 150 if (server_path == NULL) { 112 151 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); 152 rc = ENOMEM; 153 goto error; 154 } 155 } else { 156 rc = asprintf(&server_path, "%s?%s", path, uri->query); 119 157 if (rc < 0) { 120 158 fprintf(stderr, "Failed allocating path\n"); 121 uri_destroy(uri);122 return 3;159 rc = ENOMEM; 160 goto error; 123 161 } 124 162 } … … 128 166 if (req == NULL) { 129 167 fprintf(stderr, "Failed creating request\n"); 130 uri_destroy(uri);131 return 3;132 } 133 134 intrc = http_headers_append(&req->headers, "Host", uri->host);168 rc = ENOMEM; 169 goto error; 170 } 171 172 rc = http_headers_append(&req->headers, "Host", uri->host); 135 173 if (rc != EOK) { 136 174 fprintf(stderr, "Failed setting Host header: %s\n", str_error(rc)); 137 uri_destroy(uri); 138 return rc; 175 goto error; 139 176 } 140 177 … … 142 179 if (rc != EOK) { 143 180 fprintf(stderr, "Failed creating User-Agent header: %s\n", str_error(rc)); 144 uri_destroy(uri); 145 return rc; 181 goto error; 146 182 } 147 183 148 184 http_t *http = http_create(uri->host, port); 149 185 if (http == NULL) { 150 uri_destroy(uri);151 186 fprintf(stderr, "Failed creating HTTP object\n"); 152 return 3; 187 rc = ENOMEM; 188 goto error; 153 189 } 154 190 … … 156 192 if (rc != EOK) { 157 193 fprintf(stderr, "Failed connecting: %s\n", str_error(rc)); 158 uri_destroy(uri);159 return rc;194 rc = EIO; 195 goto error; 160 196 } 161 197 … … 163 199 if (rc != EOK) { 164 200 fprintf(stderr, "Failed sending request: %s\n", str_error(rc)); 165 uri_destroy(uri);166 return rc;201 rc = EIO; 202 goto error; 167 203 } 168 204 … … 172 208 if (rc != EOK) { 173 209 fprintf(stderr, "Failed receiving response: %s\n", str_error(rc)); 174 uri_destroy(uri);175 return rc;210 rc = EIO; 211 goto error; 176 212 } 177 213 … … 179 215 fprintf(stderr, "Server returned status %d %s\n", response->status, 180 216 response->message); 181 } 182 else { 183 size_t buf_size = 4096; 184 void *buf = malloc(buf_size); 217 } else { 218 buf = malloc(buf_size); 185 219 if (buf == NULL) { 186 220 fprintf(stderr, "Failed allocating buffer\n)"); 187 uri_destroy(uri);188 return ENOMEM;221 rc = ENOMEM; 222 goto error; 189 223 } 190 224 191 225 int body_size; 192 226 while ((body_size = recv_buffer(&http->recv_buffer, buf, buf_size)) > 0) { 193 fwrite(buf, 1, body_size, stdout);227 fwrite(buf, 1, body_size, ofile != NULL ? ofile : stdout); 194 228 } 195 229 … … 199 233 } 200 234 235 free(buf); 201 236 uri_destroy(uri); 237 if (fclose(ofile) != 0) { 238 printf("Error writing '%s'.\n", ofname); 239 return EIO; 240 } 241 202 242 return EOK; 243 error: 244 free(buf); 245 if (uri != NULL) 246 uri_destroy(uri); 247 if (ofile != NULL) 248 fclose(ofile); 249 return rc; 203 250 } 204 251
Note:
See TracChangeset
for help on using the changeset viewer.