Changes in uspace/app/download/main.c [fab2746:9713b0b] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/download/main.c
rfab2746 r9713b0b 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 http_t *http = NULL; 75 int rc; 76 77 if (argc < 2) { 69 78 syntax_print(); 70 return 2; 71 } 72 73 uri_t *uri = uri_parse(argv[1]); 79 rc = EINVAL; 80 goto error; 81 } 82 83 i = 1; 84 85 if (str_cmp(argv[i], "-o") == 0) { 86 ++i; 87 if (argc < i + 1) { 88 syntax_print(); 89 rc = EINVAL; 90 goto error; 91 } 92 93 ofname = argv[i++]; 94 ofile = fopen(ofname, "wb"); 95 if (ofile == NULL) { 96 fprintf(stderr, "Error creating '%s'.\n", ofname); 97 rc = EINVAL; 98 goto error; 99 } 100 } 101 102 if (argc != i + 1) { 103 syntax_print(); 104 rc = EINVAL; 105 goto error; 106 } 107 108 uri = uri_parse(argv[i]); 74 109 if (uri == NULL) { 75 110 fprintf(stderr, "Failed parsing URI\n"); 76 return 2; 111 rc = EINVAL; 112 goto error; 77 113 } 78 114 79 115 if (!uri_validate(uri)) { 80 116 fprintf(stderr, "The URI is invalid\n"); 81 return 2; 117 rc = EINVAL; 118 goto error; 82 119 } 83 120 … … 86 123 if (str_cmp(uri->scheme, "http") != 0) { 87 124 fprintf(stderr, "Only http scheme is supported at the moment\n"); 88 return 2; 125 rc = EINVAL; 126 goto error; 89 127 } 90 128 91 129 if (uri->host == NULL) { 92 130 fprintf(stderr, "host not set\n"); 93 return 2; 131 rc = EINVAL; 132 goto error; 94 133 } 95 134 96 135 uint16_t port = 80; 97 136 if (uri->port != NULL) { 98 intrc = str_uint16_t(uri->port, NULL, 10, true, &port);137 rc = str_uint16_t(uri->port, NULL, 10, true, &port); 99 138 if (rc != EOK) { 100 139 fprintf(stderr, "Invalid port number: %s\n", uri->port); 101 return 2; 140 rc = EINVAL; 141 goto error; 102 142 } 103 143 } … … 111 151 if (server_path == NULL) { 112 152 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); 153 rc = ENOMEM; 154 goto error; 155 } 156 } else { 157 rc = asprintf(&server_path, "%s?%s", path, uri->query); 119 158 if (rc < 0) { 120 159 fprintf(stderr, "Failed allocating path\n"); 121 uri_destroy(uri);122 return 3;160 rc = ENOMEM; 161 goto error; 123 162 } 124 163 } … … 128 167 if (req == NULL) { 129 168 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);169 rc = ENOMEM; 170 goto error; 171 } 172 173 rc = http_headers_append(&req->headers, "Host", uri->host); 135 174 if (rc != EOK) { 136 175 fprintf(stderr, "Failed setting Host header: %s\n", str_error(rc)); 137 uri_destroy(uri); 138 return rc; 176 goto error; 139 177 } 140 178 … … 142 180 if (rc != EOK) { 143 181 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); 182 goto error; 183 } 184 185 http = http_create(uri->host, port); 149 186 if (http == NULL) { 150 uri_destroy(uri);151 187 fprintf(stderr, "Failed creating HTTP object\n"); 152 return 3; 188 rc = ENOMEM; 189 goto error; 153 190 } 154 191 … … 156 193 if (rc != EOK) { 157 194 fprintf(stderr, "Failed connecting: %s\n", str_error(rc)); 158 uri_destroy(uri);159 return rc;195 rc = EIO; 196 goto error; 160 197 } 161 198 … … 163 200 if (rc != EOK) { 164 201 fprintf(stderr, "Failed sending request: %s\n", str_error(rc)); 165 uri_destroy(uri);166 return rc;202 rc = EIO; 203 goto error; 167 204 } 168 205 … … 172 209 if (rc != EOK) { 173 210 fprintf(stderr, "Failed receiving response: %s\n", str_error(rc)); 174 uri_destroy(uri);175 return rc;211 rc = EIO; 212 goto error; 176 213 } 177 214 … … 179 216 fprintf(stderr, "Server returned status %d %s\n", response->status, 180 217 response->message); 181 } 182 else { 183 size_t buf_size = 4096; 184 void *buf = malloc(buf_size); 218 } else { 219 buf = malloc(buf_size); 185 220 if (buf == NULL) { 186 221 fprintf(stderr, "Failed allocating buffer\n)"); 187 uri_destroy(uri);188 return ENOMEM;222 rc = ENOMEM; 223 goto error; 189 224 } 190 225 191 226 int body_size; 192 227 while ((body_size = recv_buffer(&http->recv_buffer, buf, buf_size)) > 0) { 193 fwrite(buf, 1, body_size, stdout);228 fwrite(buf, 1, body_size, ofile != NULL ? ofile : stdout); 194 229 } 195 230 … … 199 234 } 200 235 236 free(buf); 237 http_destroy(http); 201 238 uri_destroy(uri); 239 if (ofile != NULL && fclose(ofile) != 0) { 240 printf("Error writing '%s'.\n", ofname); 241 return EIO; 242 } 243 202 244 return EOK; 245 error: 246 free(buf); 247 if (http != NULL) 248 http_destroy(http); 249 if (uri != NULL) 250 uri_destroy(uri); 251 if (ofile != NULL) 252 fclose(ofile); 253 return rc; 203 254 } 204 255
Note:
See TracChangeset
for help on using the changeset viewer.