Changeset 6160

Show
Ignore:
Timestamp:
05/14/10 14:20:35 (3 years ago)
Author:
Cyrus
Message:

ustream: Initial featureset complete

Location:
luci2/libustream
Files:
4 added
1 removed
9 modified

Legend:

Unmodified
Added
Removed
  • luci2/libustream/cli/ustream-cli.c

    r6158 r6160  
     1/** 
     2 *   ustream - Micro URL I/O stream library 
     3 *   Copyright (C) 2010 Steven Barth <steven@midlink.org> 
     4 *   Copyright (C) 2010 John Crispin <blogic@openwrt.org> 
     5 * 
     6 *   This program is free software; you can redistribute it and/or modify 
     7 *   it under the terms of the GNU General Public License as published by 
     8 *   the Free Software Foundation; either version 2 of the License, or 
     9 *   (at your option) any later version. 
     10 * 
     11 *   This program is distributed in the hope that it will be useful, 
     12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 *   GNU General Public License for more details. 
     15 * 
     16 *   You should have received a copy of the GNU General Public License 
     17 *   along with this program; if not, write to the Free Software 
     18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
     19 * 
     20 */ 
     21 
    122#define _GNU_SOURCE 
    223#include <ustream.h> 
     
    1738        "Usage: %s [options] [command] url\n\n" 
    1839        "Commands:\n" 
    19         "   get             Open URL for reading (default)\n" 
    20         "   put             Open URL for writing\n" 
    21         "   post                Open URL for application call\n" 
     40        "   get         Open URL for reading (default)\n" 
     41        "   put         Open URL for writing\n" 
     42        "   post            Open URL for application call\n" 
    2243        "\n\nOptions:\n" 
    23         "   -A              Aggregate data (for broken HTTP servers)\n" 
    24         "   -c              Continue transfer (get only)\n" 
    25         "   -h              Show this help message\n" 
    26         "   -o <offset>         Set source offset\n" 
    27         "   -s <size>           Set source size\n" 
    28         "   -v              Increase verbosity\n" 
    29         "   -H <key>:<value>        Set header\n" 
    30         "   -I <path>           Read from source (default: stdin)\n" 
    31         "   -O <path>           Save to destination (default: stdout)\n", 
     44        "   -A          Aggregate data (for broken HTTP servers)\n" 
     45        "   -c          Continue transfer (get only)\n" 
     46        "   -h          Show this help message\n" 
     47        "   -o <offset>     Set source offset\n" 
     48        "   -s <size>       Set source size\n" 
     49        "   -v          Increase verbosity\n" 
     50        "   -H <key>:<value>    Set header\n" 
     51        "   -I <path>       Read from source (default: stdin)\n" 
     52        "   -O <path>       Save to destination (default: stdout)\n", 
    3253        app); 
    3354    return 1; 
     
    86107    } 
    87108 
    88     const char *actstr = argv[1]; 
     109    const char *actstr = argv[optind]; 
    89110    const char *url = actstr; 
    90111    int action = USTREAM_GET; 
     
    92113    if (!strcasecmp(actstr, "put")) { 
    93114        action = USTREAM_PUT; 
    94         url = argv[2]; 
     115        url = argv[optind + 1]; 
    95116    } else if (!strcasecmp(actstr, "post")) { 
    96117        action = USTREAM_POST; 
    97         url = argv[2]; 
    98     } else if (strcasecmp(actstr, "get") && !strstr(actstr, "://")) { 
     118        url = argv[optind + 1]; 
     119    } else if (!strcasecmp(actstr, "get")) { 
     120        action = USTREAM_GET; 
     121        url = argv[optind + 1]; 
     122    } else if (!strstr(actstr, "://")) { 
    99123        return usage(*argv); 
    100124    } 
     
    131155            goto error; 
    132156        } 
     157        size = (uint64_t)reqsize; 
    133158    } 
    134159 
     
    165190 
    166191    ssize_t rxed; 
    167     while ((rxed = ustream_read(stream, buffer, sizeof(buffer)))) { 
     192    while ((rxed = ustream_read(stream, buffer, sizeof(buffer))) > 0) { 
    168193        fwrite(buffer, 1, rxed, fpout); 
     194    } 
     195    if (rxed < 0) { 
     196        goto error; 
    169197    } 
    170198 
  • luci2/libustream/core.c

    r6156 r6160  
    1010 
    1111static int ustream_io_timeout = 60; 
    12  
    13 static unsigned char b64encode_tbl[] = 
    14     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
    15  
    16 USTREAM_LOCAL char* ustream_b64encode(const void *in, size_t len) { 
    17     size_t lenout, pad, i; 
    18     const uint8_t *data = (const uint8_t*)in; 
    19  
    20     lenout = len / 3; 
    21     lenout *= 4; 
    22     pad = len % 3; 
    23  
    24     if (len == 0) { 
    25         return strdup(""); 
    26     } else if (pad) { 
    27         lenout += 4; 
    28     } 
    29  
    30     char *out = malloc(lenout + 1); 
    31     if (!out) { 
    32         return NULL; 
    33     } 
    34  
    35     uint8_t *o = (uint8_t*)out; 
    36     for (i = 0; i < len; i += 3) { 
    37         uint32_t cv = (data[i] << 16) | (data[i+1] << 8) | data[i+2]; 
    38         *(o+3) = b64encode_tbl[ cv        & 0x3f]; 
    39         *(o+2) = b64encode_tbl[(cv >> 6)  & 0x3f]; 
    40         *(o+1) = b64encode_tbl[(cv >> 12) & 0x3f]; 
    41         *o     = b64encode_tbl[(cv >> 18) & 0x3f]; 
    42         o += 4; 
    43     } 
    44  
    45     if (pad) { 
    46         uint32_t cv = data[len-pad] << 16; 
    47         *(o-1) = '='; 
    48         *(o-2) = '='; 
    49         if (pad == 2) { 
    50             cv |= data[len-pad+1] << 8; 
    51             *(o-2) = b64encode_tbl[(cv >> 6) & 0x3f]; 
    52         } 
    53         *(o-3) = b64encode_tbl[(cv >> 12) & 0x3f]; 
    54         *(o-4) = b64encode_tbl[(cv >> 18) & 0x3f]; 
    55     } 
    56  
    57     out[lenout] = 0; 
    58     return out; 
    59 } 
    6012 
    6113USTREAM_LOCAL void ustream_url_parse 
  • luci2/libustream/core.h

    r6158 r6160  
    1313#include <unistd.h> 
    1414 
    15 #define USTREAM_BANNER "ustream by Steven Barth & John Crispin" 
     15#define USTREAM_BANNER "ustream/luci2 by Steven Barth & John Crispin" 
    1616 
    1717#define USTREAM_DATA_READABLE       0x0001 
     
    6363USTREAM_LOCAL void ustream_url_parse 
    6464(ustream_t *stream, const char *url, size_t plen); 
    65 USTREAM_LOCAL char* ustream_b64encode(const void *in, size_t len); 
    6665USTREAM_LOCAL int ustream_io_socket (const char *host, const char *service); 
    6766 
  • luci2/libustream/extension/json.c

    r6158 r6160  
     1/** 
     2 *   ustream - Micro URL I/O stream library 
     3 *   Copyright (C) 2010 Steven Barth <steven@midlink.org> 
     4 *   Copyright (C) 2010 John Crispin <blogic@openwrt.org> 
     5 * 
     6 *   This program is free software; you can redistribute it and/or modify 
     7 *   it under the terms of the GNU General Public License as published by 
     8 *   the Free Software Foundation; either version 2 of the License, or 
     9 *   (at your option) any later version. 
     10 * 
     11 *   This program is distributed in the hope that it will be useful, 
     12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 *   GNU General Public License for more details. 
     15 * 
     16 *   You should have received a copy of the GNU General Public License 
     17 *   along with this program; if not, write to the Free Software 
     18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
     19 * 
     20 */ 
     21 
    122#define _GNU_SOURCE 
    223#include <stdio.h> 
    324#include <string.h> 
    4 #include <json/json.h> 
    5 #include "ustream-json.h" 
    6 #include "../ustream.h" 
     25#include "../ustream-json.h" 
    726 
    827json_object* ustream_json_post(const char *url, json_object *req) { 
  • luci2/libustream/protocol/http.c

    r6158 r6160  
    99#include <unistd.h> 
    1010#include "../core.h" 
     11#include "../ustream-encoding.h" 
    1112 
    1213#define HTTP_FOLLOW_DEFAULT 5 
     
    8182 
    8283    if (stream->auth) { 
     84        char *auth = ustream_encoding_urldecode(stream->auth, 0); 
     85        if (!auth) { 
     86            free(auth); 
     87            free(http); 
     88            return -(errno = EINVAL); 
     89        } 
    8390        char buf[256] = "Basic ", *bd; 
    84         if ((bd = ustream_b64encode(stream->auth, strlen(stream->auth)))) { 
     91        size_t authlen = strlen(auth); 
     92        if ((bd = ustream_encoding_b64encode(auth, &authlen))) { 
    8593            strncat(buf, bd, sizeof(buf) - strlen(buf) - 1); 
    8694            free(bd); 
    8795            ustream_uhtbl_set(&http->header, "Authorization", buf, strlen(buf)); 
    8896        } 
     97        free(auth); 
    8998    } 
    9099 
     
    533542                len -= sendc; 
    534543                buf = (char*)buf + sendc; 
    535                 if ((http->chunkremain -= sendc) == 0) { 
     544                if ((stream->status & HTTP_WRITE_CHUNKED) 
     545                && (http->chunkremain -= sendc) == 0) { 
    536546                    fputs("\r\n", http->transport); 
    537547                } 
  • luci2/libustream/tls.c

    r6153 r6160  
     1/** 
     2 *   ustream - Micro URL I/O stream library 
     3 *   Copyright (C) 2010 Steven Barth <steven@midlink.org> 
     4 *   Copyright (C) 2010 John Crispin <blogic@openwrt.org> 
     5 * 
     6 *   This program is free software; you can redistribute it and/or modify 
     7 *   it under the terms of the GNU General Public License as published by 
     8 *   the Free Software Foundation; either version 2 of the License, or 
     9 *   (at your option) any later version. 
     10 * 
     11 *   This program is distributed in the hope that it will be useful, 
     12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 *   GNU General Public License for more details. 
     15 * 
     16 *   You should have received a copy of the GNU General Public License 
     17 *   along with this program; if not, write to the Free Software 
     18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
     19 * 
     20 */ 
     21 
    122#define _GNU_SOURCE 
    223#include "core.h" 
  • luci2/libustream/uhtbl.c

    r6158 r6160  
     1/** 
     2 *   ustream - Micro URL I/O stream library 
     3 *   Copyright (C) 2010 Steven Barth <steven@midlink.org> 
     4 *   Copyright (C) 2010 John Crispin <blogic@openwrt.org> 
     5 * 
     6 *   This program is free software; you can redistribute it and/or modify 
     7 *   it under the terms of the GNU General Public License as published by 
     8 *   the Free Software Foundation; either version 2 of the License, or 
     9 *   (at your option) any later version. 
     10 * 
     11 *   This program is distributed in the hope that it will be useful, 
     12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 *   GNU General Public License for more details. 
     15 * 
     16 *   You should have received a copy of the GNU General Public License 
     17 *   along with this program; if not, write to the Free Software 
     18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
     19 * 
     20 */ 
     21 
    122#include <stdlib.h> 
    223#include <string.h> 
  • luci2/libustream/ustream.c

    r6155 r6160  
     1/** 
     2 *   ustream - Micro URL I/O stream library 
     3 *   Copyright (C) 2010 Steven Barth <steven@midlink.org> 
     4 *   Copyright (C) 2010 John Crispin <blogic@openwrt.org> 
     5 * 
     6 *   This program is free software; you can redistribute it and/or modify 
     7 *   it under the terms of the GNU General Public License as published by 
     8 *   the Free Software Foundation; either version 2 of the License, or 
     9 *   (at your option) any later version. 
     10 * 
     11 *   This program is distributed in the hope that it will be useful, 
     12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 *   GNU General Public License for more details. 
     15 * 
     16 *   You should have received a copy of the GNU General Public License 
     17 *   along with this program; if not, write to the Free Software 
     18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
     19 * 
     20 */ 
     21 
    122#include <stdint.h> 
    223#include <stdio.h> 
  • luci2/libustream/ustream.h

    r6158 r6160  
     1/** 
     2 *   ustream - Micro URL I/O stream library 
     3 *   Copyright (C) 2010 Steven Barth <steven@midlink.org> 
     4 *   Copyright (C) 2010 John Crispin <blogic@openwrt.org> 
     5 * 
     6 *   This program is free software; you can redistribute it and/or modify 
     7 *   it under the terms of the GNU General Public License as published by 
     8 *   the Free Software Foundation; either version 2 of the License, or 
     9 *   (at your option) any later version. 
     10 * 
     11 *   This program is distributed in the hope that it will be useful, 
     12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
     13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
     14 *   GNU General Public License for more details. 
     15 * 
     16 *   You should have received a copy of the GNU General Public License 
     17 *   along with this program; if not, write to the Free Software 
     18 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
     19 * 
     20 */ 
     21 
    122#ifndef USTREAM_H_ 
    223#define USTREAM_H_