Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/download/main.c

    r9713b0b rfab2746  
    5757static void syntax_print(void)
    5858{
    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");
     59        fprintf(stderr, "Usage: download <url>\n");
     60        fprintf(stderr, "  This will write the data to stdout, so you may want\n");
    6161        fprintf(stderr, "  to redirect the output, e.g.\n");
    6262        fprintf(stderr, "\n");
     
    6666int main(int argc, char *argv[])
    6767{
    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) {
    7869                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]);
    10974        if (uri == NULL) {
    11075                fprintf(stderr, "Failed parsing URI\n");
    111                 rc = EINVAL;
    112                 goto error;
     76                return 2;
    11377        }
    11478       
    11579        if (!uri_validate(uri)) {
    11680                fprintf(stderr, "The URI is invalid\n");
    117                 rc = EINVAL;
    118                 goto error;
     81                return 2;
    11982        }
    12083       
     
    12386        if (str_cmp(uri->scheme, "http") != 0) {
    12487                fprintf(stderr, "Only http scheme is supported at the moment\n");
    125                 rc = EINVAL;
    126                 goto error;
     88                return 2;
    12789        }
    12890       
    12991        if (uri->host == NULL) {
    13092                fprintf(stderr, "host not set\n");
    131                 rc = EINVAL;
    132                 goto error;
     93                return 2;
    13394        }
    13495       
    13596        uint16_t port = 80;
    13697        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);
    13899                if (rc != EOK) {
    139100                        fprintf(stderr, "Invalid port number: %s\n", uri->port);
    140                         rc = EINVAL;
    141                         goto error;
     101                        return 2;
    142102                }
    143103        }
     
    151111                if (server_path == NULL) {
    152112                        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);
    158119                if (rc < 0) {
    159120                        fprintf(stderr, "Failed allocating path\n");
    160                         rc = ENOMEM;
    161                         goto error;
     121                        uri_destroy(uri);
     122                        return 3;
    162123                }
    163124        }
     
    167128        if (req == NULL) {
    168129                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);
    174135        if (rc != EOK) {
    175136                fprintf(stderr, "Failed setting Host header: %s\n", str_error(rc));
    176                 goto error;
     137                uri_destroy(uri);
     138                return rc;
    177139        }
    178140       
     
    180142        if (rc != EOK) {
    181143                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);
    186149        if (http == NULL) {
     150                uri_destroy(uri);
    187151                fprintf(stderr, "Failed creating HTTP object\n");
    188                 rc = ENOMEM;
    189                 goto error;
     152                return 3;
    190153        }
    191154       
     
    193156        if (rc != EOK) {
    194157                fprintf(stderr, "Failed connecting: %s\n", str_error(rc));
    195                 rc = EIO;
    196                 goto error;
     158                uri_destroy(uri);
     159                return rc;
    197160        }
    198161       
     
    200163        if (rc != EOK) {
    201164                fprintf(stderr, "Failed sending request: %s\n", str_error(rc));
    202                 rc = EIO;
    203                 goto error;
     165                uri_destroy(uri);
     166                return rc;
    204167        }
    205168       
     
    209172        if (rc != EOK) {
    210173                fprintf(stderr, "Failed receiving response: %s\n", str_error(rc));
    211                 rc = EIO;
    212                 goto error;
     174                uri_destroy(uri);
     175                return rc;
    213176        }
    214177       
     
    216179                fprintf(stderr, "Server returned status %d %s\n", response->status,
    217180                    response->message);
    218         } else {
    219                 buf = malloc(buf_size);
     181        }
     182        else {
     183                size_t buf_size = 4096;
     184                void *buf = malloc(buf_size);
    220185                if (buf == NULL) {
    221186                        fprintf(stderr, "Failed allocating buffer\n)");
    222                         rc = ENOMEM;
    223                         goto error;
     187                        uri_destroy(uri);
     188                        return ENOMEM;
    224189                }
    225190               
    226191                int body_size;
    227192                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);
    229194                }
    230195               
     
    234199        }
    235200       
    236         free(buf);
    237         http_destroy(http);
    238201        uri_destroy(uri);
    239         if (ofile != NULL && fclose(ofile) != 0) {
    240                 printf("Error writing '%s'.\n", ofname);
    241                 return EIO;
    242         }
    243 
    244202        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;
    254203}
    255204
Note: See TracChangeset for help on using the changeset viewer.