Changeset a35b458 in mainline for uspace/app/download/main.c
- Timestamp:
- 2018-03-02T20:10:49Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f1380b7
- Parents:
- 3061bc1
- git-author:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-02-28 17:38:31)
- git-committer:
- Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:10:49)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/download/main.c
r3061bc1 ra35b458 81 81 goto error; 82 82 } 83 83 84 84 i = 1; 85 85 86 86 if (str_cmp(argv[i], "-o") == 0) { 87 87 ++i; … … 91 91 goto error; 92 92 } 93 93 94 94 ofname = argv[i++]; 95 95 ofile = fopen(ofname, "wb"); … … 100 100 } 101 101 } 102 102 103 103 if (argc != i + 1) { 104 104 syntax_print(); … … 106 106 goto error; 107 107 } 108 108 109 109 uri = uri_parse(argv[i]); 110 110 if (uri == NULL) { … … 113 113 goto error; 114 114 } 115 115 116 116 if (!uri_validate(uri)) { 117 117 fprintf(stderr, "The URI is invalid\n"); … … 119 119 goto error; 120 120 } 121 121 122 122 /* TODO uri_normalize(uri) */ 123 123 124 124 if (str_cmp(uri->scheme, "http") != 0) { 125 125 fprintf(stderr, "Only http scheme is supported at the moment\n"); … … 127 127 goto error; 128 128 } 129 129 130 130 if (uri->host == NULL) { 131 131 fprintf(stderr, "host not set\n"); … … 133 133 goto error; 134 134 } 135 135 136 136 uint16_t port = 80; 137 137 if (uri->port != NULL) { … … 143 143 } 144 144 } 145 145 146 146 const char *path = uri->path; 147 147 if (path == NULL || *path == 0) … … 163 163 } 164 164 } 165 165 166 166 http_request_t *req = http_request_create("GET", server_path); 167 167 free(server_path); … … 171 171 goto error; 172 172 } 173 173 174 174 rc = http_headers_append(&req->headers, "Host", uri->host); 175 175 if (rc != EOK) { … … 177 177 goto error; 178 178 } 179 179 180 180 rc = http_headers_append(&req->headers, "User-Agent", USER_AGENT); 181 181 if (rc != EOK) { … … 183 183 goto error; 184 184 } 185 185 186 186 http = http_create(uri->host, port); 187 187 if (http == NULL) { … … 190 190 goto error; 191 191 } 192 192 193 193 rc = http_connect(http); 194 194 if (rc != EOK) { … … 197 197 goto error; 198 198 } 199 199 200 200 rc = http_send_request(http, req); 201 201 if (rc != EOK) { … … 204 204 goto error; 205 205 } 206 206 207 207 http_response_t *response = NULL; 208 208 rc = http_receive_response(&http->recv_buffer, &response, 16 * 1024, … … 213 213 goto error; 214 214 } 215 215 216 216 if (response->status != 200) { 217 217 fprintf(stderr, "Server returned status %d %s\n", response->status, … … 224 224 goto error; 225 225 } 226 226 227 227 size_t body_size; 228 228 while ((rc = recv_buffer(&http->recv_buffer, buf, buf_size, &body_size)) == EOK && body_size > 0) { 229 229 fwrite(buf, 1, body_size, ofile != NULL ? ofile : stdout); 230 230 } 231 231 232 232 if (rc != EOK) { 233 233 fprintf(stderr, "Failed receiving body: %s", str_error(rc)); 234 234 } 235 235 } 236 236 237 237 free(buf); 238 238 http_destroy(http);
Note:
See TracChangeset
for help on using the changeset viewer.