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