Changeset 6160
- Timestamp:
- 05/14/10 14:20:35 (3 years ago)
- Location:
- luci2/libustream
- Files:
-
- 4 added
- 1 removed
- 9 modified
-
cli/ustream-cli.c (modified) (6 diffs)
-
core.c (modified) (1 diff)
-
core.h (modified) (2 diffs)
-
encoding.c (added)
-
extension/json.c (modified) (1 diff)
-
extension/ustream-json.h (deleted)
-
protocol/http.c (modified) (3 diffs)
-
tls.c (modified) (1 diff)
-
uattr.h (added)
-
uhtbl.c (modified) (1 diff)
-
ustream-encoding.h (added)
-
ustream-json.h (added)
-
ustream.c (modified) (1 diff)
-
ustream.h (modified) (1 diff)
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 1 22 #define _GNU_SOURCE 2 23 #include <ustream.h> … … 17 38 "Usage: %s [options] [command] url\n\n" 18 39 "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" 22 43 "\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", 32 53 app); 33 54 return 1; … … 86 107 } 87 108 88 const char *actstr = argv[ 1];109 const char *actstr = argv[optind]; 89 110 const char *url = actstr; 90 111 int action = USTREAM_GET; … … 92 113 if (!strcasecmp(actstr, "put")) { 93 114 action = USTREAM_PUT; 94 url = argv[ 2];115 url = argv[optind + 1]; 95 116 } else if (!strcasecmp(actstr, "post")) { 96 117 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, "://")) { 99 123 return usage(*argv); 100 124 } … … 131 155 goto error; 132 156 } 157 size = (uint64_t)reqsize; 133 158 } 134 159 … … 165 190 166 191 ssize_t rxed; 167 while ((rxed = ustream_read(stream, buffer, sizeof(buffer))) ) {192 while ((rxed = ustream_read(stream, buffer, sizeof(buffer))) > 0) { 168 193 fwrite(buffer, 1, rxed, fpout); 194 } 195 if (rxed < 0) { 196 goto error; 169 197 } 170 198 -
luci2/libustream/core.c
r6156 r6160 10 10 11 11 static 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 }60 12 61 13 USTREAM_LOCAL void ustream_url_parse -
luci2/libustream/core.h
r6158 r6160 13 13 #include <unistd.h> 14 14 15 #define USTREAM_BANNER "ustream by Steven Barth & John Crispin"15 #define USTREAM_BANNER "ustream/luci2 by Steven Barth & John Crispin" 16 16 17 17 #define USTREAM_DATA_READABLE 0x0001 … … 63 63 USTREAM_LOCAL void ustream_url_parse 64 64 (ustream_t *stream, const char *url, size_t plen); 65 USTREAM_LOCAL char* ustream_b64encode(const void *in, size_t len);66 65 USTREAM_LOCAL int ustream_io_socket (const char *host, const char *service); 67 66 -
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 1 22 #define _GNU_SOURCE 2 23 #include <stdio.h> 3 24 #include <string.h> 4 #include <json/json.h> 5 #include "ustream-json.h" 6 #include "../ustream.h" 25 #include "../ustream-json.h" 7 26 8 27 json_object* ustream_json_post(const char *url, json_object *req) { -
luci2/libustream/protocol/http.c
r6158 r6160 9 9 #include <unistd.h> 10 10 #include "../core.h" 11 #include "../ustream-encoding.h" 11 12 12 13 #define HTTP_FOLLOW_DEFAULT 5 … … 81 82 82 83 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 } 83 90 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))) { 85 93 strncat(buf, bd, sizeof(buf) - strlen(buf) - 1); 86 94 free(bd); 87 95 ustream_uhtbl_set(&http->header, "Authorization", buf, strlen(buf)); 88 96 } 97 free(auth); 89 98 } 90 99 … … 533 542 len -= sendc; 534 543 buf = (char*)buf + sendc; 535 if ((http->chunkremain -= sendc) == 0) { 544 if ((stream->status & HTTP_WRITE_CHUNKED) 545 && (http->chunkremain -= sendc) == 0) { 536 546 fputs("\r\n", http->transport); 537 547 } -
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 1 22 #define _GNU_SOURCE 2 23 #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 1 22 #include <stdlib.h> 2 23 #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 1 22 #include <stdint.h> 2 23 #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 1 22 #ifndef USTREAM_H_ 2 23 #define USTREAM_H_
