Ignore:
File:
1 edited

Legend:

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

    rfab2746 r9713b0b  
    5757static void syntax_print(void)
    5858{
    59         fprintf(stderr, "Usage: download <url>\n");
    60         fprintf(stderr, "  This will write the data to 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");
    6161        fprintf(stderr, "  to redirect the output, e.g.\n");
    6262        fprintf(stderr, "\n");
     
    6666int main(int argc, char *argv[])
    6767{
    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) {
    6978                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]);
    74109        if (uri == NULL) {
    75110                fprintf(stderr, "Failed parsing URI\n");
    76                 return 2;
     111                rc = EINVAL;
     112                goto error;
    77113        }
    78114       
    79115        if (!uri_validate(uri)) {
    80116                fprintf(stderr, "The URI is invalid\n");
    81                 return 2;
     117                rc = EINVAL;
     118                goto error;
    82119        }
    83120       
     
    86123        if (str_cmp(uri->scheme, "http") != 0) {
    87124                fprintf(stderr, "Only http scheme is supported at the moment\n");
    88                 return 2;
     125                rc = EINVAL;
     126                goto error;
    89127        }
    90128       
    91129        if (uri->host == NULL) {
    92130                fprintf(stderr, "host not set\n");
    93                 return 2;
     131                rc = EINVAL;
     132                goto error;
    94133        }
    95134       
    96135        uint16_t port = 80;
    97136        if (uri->port != NULL) {
    98                 int rc = str_uint16_t(uri->port, NULL, 10, true, &port);
     137                rc = str_uint16_t(uri->port, NULL, 10, true, &port);
    99138                if (rc != EOK) {
    100139                        fprintf(stderr, "Invalid port number: %s\n", uri->port);
    101                         return 2;
     140                        rc = EINVAL;
     141                        goto error;
    102142                }
    103143        }
     
    111151                if (server_path == NULL) {
    112152                        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);
    119158                if (rc < 0) {
    120159                        fprintf(stderr, "Failed allocating path\n");
    121                         uri_destroy(uri);
    122                         return 3;
     160                        rc = ENOMEM;
     161                        goto error;
    123162                }
    124163        }
     
    128167        if (req == NULL) {
    129168                fprintf(stderr, "Failed creating request\n");
    130                 uri_destroy(uri);
    131                 return 3;
    132         }
    133        
    134         int rc = 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);
    135174        if (rc != EOK) {
    136175                fprintf(stderr, "Failed setting Host header: %s\n", str_error(rc));
    137                 uri_destroy(uri);
    138                 return rc;
     176                goto error;
    139177        }
    140178       
     
    142180        if (rc != EOK) {
    143181                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);
    149186        if (http == NULL) {
    150                 uri_destroy(uri);
    151187                fprintf(stderr, "Failed creating HTTP object\n");
    152                 return 3;
     188                rc = ENOMEM;
     189                goto error;
    153190        }
    154191       
     
    156193        if (rc != EOK) {
    157194                fprintf(stderr, "Failed connecting: %s\n", str_error(rc));
    158                 uri_destroy(uri);
    159                 return rc;
     195                rc = EIO;
     196                goto error;
    160197        }
    161198       
     
    163200        if (rc != EOK) {
    164201                fprintf(stderr, "Failed sending request: %s\n", str_error(rc));
    165                 uri_destroy(uri);
    166                 return rc;
     202                rc = EIO;
     203                goto error;
    167204        }
    168205       
     
    172209        if (rc != EOK) {
    173210                fprintf(stderr, "Failed receiving response: %s\n", str_error(rc));
    174                 uri_destroy(uri);
    175                 return rc;
     211                rc = EIO;
     212                goto error;
    176213        }
    177214       
     
    179216                fprintf(stderr, "Server returned status %d %s\n", response->status,
    180217                    response->message);
    181         }
    182         else {
    183                 size_t buf_size = 4096;
    184                 void *buf = malloc(buf_size);
     218        } else {
     219                buf = malloc(buf_size);
    185220                if (buf == NULL) {
    186221                        fprintf(stderr, "Failed allocating buffer\n)");
    187                         uri_destroy(uri);
    188                         return ENOMEM;
     222                        rc = ENOMEM;
     223                        goto error;
    189224                }
    190225               
    191226                int body_size;
    192227                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);
    194229                }
    195230               
     
    199234        }
    200235       
     236        free(buf);
     237        http_destroy(http);
    201238        uri_destroy(uri);
     239        if (ofile != NULL && fclose(ofile) != 0) {
     240                printf("Error writing '%s'.\n", ofname);
     241                return EIO;
     242        }
     243
    202244        return EOK;
     245error:
     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;
    203254}
    204255
Note: See TracChangeset for help on using the changeset viewer.