Rafal Kupiec пре 5 година
родитељ
комит
d79b294105
8 измењених фајлова са 1474 додато и 4 уклоњено
  1. 339
    0
      COPYING
  2. 44
    0
      Makefile
  3. 21
    0
      README
  4. 0
    4
      README.md
  5. 823
    0
      libweb.c
  6. 79
    0
      libweb.h
  7. 89
    0
      webget.c
  8. 79
    0
      webpost.c

+ 339
- 0
COPYING Прегледај датотеку

@@ -0,0 +1,339 @@
1
+                    GNU GENERAL PUBLIC LICENSE
2
+                       Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+                            Preamble
10
+
11
+  The licenses for most software are designed to take away your
12
+freedom to share and change it.  By contrast, the GNU General Public
13
+License is intended to guarantee your freedom to share and change free
14
+software--to make sure the software is free for all its users.  This
15
+General Public License applies to most of the Free Software
16
+Foundation's software and to any other program whose authors commit to
17
+using it.  (Some other Free Software Foundation software is covered by
18
+the GNU Lesser General Public License instead.)  You can apply it to
19
+your programs, too.
20
+
21
+  When we speak of free software, we are referring to freedom, not
22
+price.  Our General Public Licenses are designed to make sure that you
23
+have the freedom to distribute copies of free software (and charge for
24
+this service if you wish), that you receive source code or can get it
25
+if you want it, that you can change the software or use pieces of it
26
+in new free programs; and that you know you can do these things.
27
+
28
+  To protect your rights, we need to make restrictions that forbid
29
+anyone to deny you these rights or to ask you to surrender the rights.
30
+These restrictions translate to certain responsibilities for you if you
31
+distribute copies of the software, or if you modify it.
32
+
33
+  For example, if you distribute copies of such a program, whether
34
+gratis or for a fee, you must give the recipients all the rights that
35
+you have.  You must make sure that they, too, receive or can get the
36
+source code.  And you must show them these terms so they know their
37
+rights.
38
+
39
+  We protect your rights with two steps: (1) copyright the software, and
40
+(2) offer you this license which gives you legal permission to copy,
41
+distribute and/or modify the software.
42
+
43
+  Also, for each author's protection and ours, we want to make certain
44
+that everyone understands that there is no warranty for this free
45
+software.  If the software is modified by someone else and passed on, we
46
+want its recipients to know that what they have is not the original, so
47
+that any problems introduced by others will not reflect on the original
48
+authors' reputations.
49
+
50
+  Finally, any free program is threatened constantly by software
51
+patents.  We wish to avoid the danger that redistributors of a free
52
+program will individually obtain patent licenses, in effect making the
53
+program proprietary.  To prevent this, we have made it clear that any
54
+patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+  The precise terms and conditions for copying, distribution and
57
+modification follow.
58
+
59
+                    GNU GENERAL PUBLIC LICENSE
60
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+  0. This License applies to any program or other work which contains
63
+a notice placed by the copyright holder saying it may be distributed
64
+under the terms of this General Public License.  The "Program", below,
65
+refers to any such program or work, and a "work based on the Program"
66
+means either the Program or any derivative work under copyright law:
67
+that is to say, a work containing the Program or a portion of it,
68
+either verbatim or with modifications and/or translated into another
69
+language.  (Hereinafter, translation is included without limitation in
70
+the term "modification".)  Each licensee is addressed as "you".
71
+
72
+Activities other than copying, distribution and modification are not
73
+covered by this License; they are outside its scope.  The act of
74
+running the Program is not restricted, and the output from the Program
75
+is covered only if its contents constitute a work based on the
76
+Program (independent of having been made by running the Program).
77
+Whether that is true depends on what the Program does.
78
+
79
+  1. You may copy and distribute verbatim copies of the Program's
80
+source code as you receive it, in any medium, provided that you
81
+conspicuously and appropriately publish on each copy an appropriate
82
+copyright notice and disclaimer of warranty; keep intact all the
83
+notices that refer to this License and to the absence of any warranty;
84
+and give any other recipients of the Program a copy of this License
85
+along with the Program.
86
+
87
+You may charge a fee for the physical act of transferring a copy, and
88
+you may at your option offer warranty protection in exchange for a fee.
89
+
90
+  2. You may modify your copy or copies of the Program or any portion
91
+of it, thus forming a work based on the Program, and copy and
92
+distribute such modifications or work under the terms of Section 1
93
+above, provided that you also meet all of these conditions:
94
+
95
+    a) You must cause the modified files to carry prominent notices
96
+    stating that you changed the files and the date of any change.
97
+
98
+    b) You must cause any work that you distribute or publish, that in
99
+    whole or in part contains or is derived from the Program or any
100
+    part thereof, to be licensed as a whole at no charge to all third
101
+    parties under the terms of this License.
102
+
103
+    c) If the modified program normally reads commands interactively
104
+    when run, you must cause it, when started running for such
105
+    interactive use in the most ordinary way, to print or display an
106
+    announcement including an appropriate copyright notice and a
107
+    notice that there is no warranty (or else, saying that you provide
108
+    a warranty) and that users may redistribute the program under
109
+    these conditions, and telling the user how to view a copy of this
110
+    License.  (Exception: if the Program itself is interactive but
111
+    does not normally print such an announcement, your work based on
112
+    the Program is not required to print an announcement.)
113
+
114
+These requirements apply to the modified work as a whole.  If
115
+identifiable sections of that work are not derived from the Program,
116
+and can be reasonably considered independent and separate works in
117
+themselves, then this License, and its terms, do not apply to those
118
+sections when you distribute them as separate works.  But when you
119
+distribute the same sections as part of a whole which is a work based
120
+on the Program, the distribution of the whole must be on the terms of
121
+this License, whose permissions for other licensees extend to the
122
+entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+Thus, it is not the intent of this section to claim rights or contest
125
+your rights to work written entirely by you; rather, the intent is to
126
+exercise the right to control the distribution of derivative or
127
+collective works based on the Program.
128
+
129
+In addition, mere aggregation of another work not based on the Program
130
+with the Program (or with a work based on the Program) on a volume of
131
+a storage or distribution medium does not bring the other work under
132
+the scope of this License.
133
+
134
+  3. You may copy and distribute the Program (or a work based on it,
135
+under Section 2) in object code or executable form under the terms of
136
+Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+    a) Accompany it with the complete corresponding machine-readable
139
+    source code, which must be distributed under the terms of Sections
140
+    1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+    b) Accompany it with a written offer, valid for at least three
143
+    years, to give any third party, for a charge no more than your
144
+    cost of physically performing source distribution, a complete
145
+    machine-readable copy of the corresponding source code, to be
146
+    distributed under the terms of Sections 1 and 2 above on a medium
147
+    customarily used for software interchange; or,
148
+
149
+    c) Accompany it with the information you received as to the offer
150
+    to distribute corresponding source code.  (This alternative is
151
+    allowed only for noncommercial distribution and only if you
152
+    received the program in object code or executable form with such
153
+    an offer, in accord with Subsection b above.)
154
+
155
+The source code for a work means the preferred form of the work for
156
+making modifications to it.  For an executable work, complete source
157
+code means all the source code for all modules it contains, plus any
158
+associated interface definition files, plus the scripts used to
159
+control compilation and installation of the executable.  However, as a
160
+special exception, the source code distributed need not include
161
+anything that is normally distributed (in either source or binary
162
+form) with the major components (compiler, kernel, and so on) of the
163
+operating system on which the executable runs, unless that component
164
+itself accompanies the executable.
165
+
166
+If distribution of executable or object code is made by offering
167
+access to copy from a designated place, then offering equivalent
168
+access to copy the source code from the same place counts as
169
+distribution of the source code, even though third parties are not
170
+compelled to copy the source along with the object code.
171
+
172
+  4. You may not copy, modify, sublicense, or distribute the Program
173
+except as expressly provided under this License.  Any attempt
174
+otherwise to copy, modify, sublicense or distribute the Program is
175
+void, and will automatically terminate your rights under this License.
176
+However, parties who have received copies, or rights, from you under
177
+this License will not have their licenses terminated so long as such
178
+parties remain in full compliance.
179
+
180
+  5. You are not required to accept this License, since you have not
181
+signed it.  However, nothing else grants you permission to modify or
182
+distribute the Program or its derivative works.  These actions are
183
+prohibited by law if you do not accept this License.  Therefore, by
184
+modifying or distributing the Program (or any work based on the
185
+Program), you indicate your acceptance of this License to do so, and
186
+all its terms and conditions for copying, distributing or modifying
187
+the Program or works based on it.
188
+
189
+  6. Each time you redistribute the Program (or any work based on the
190
+Program), the recipient automatically receives a license from the
191
+original licensor to copy, distribute or modify the Program subject to
192
+these terms and conditions.  You may not impose any further
193
+restrictions on the recipients' exercise of the rights granted herein.
194
+You are not responsible for enforcing compliance by third parties to
195
+this License.
196
+
197
+  7. If, as a consequence of a court judgment or allegation of patent
198
+infringement or for any other reason (not limited to patent issues),
199
+conditions are imposed on you (whether by court order, agreement or
200
+otherwise) that contradict the conditions of this License, they do not
201
+excuse you from the conditions of this License.  If you cannot
202
+distribute so as to satisfy simultaneously your obligations under this
203
+License and any other pertinent obligations, then as a consequence you
204
+may not distribute the Program at all.  For example, if a patent
205
+license would not permit royalty-free redistribution of the Program by
206
+all those who receive copies directly or indirectly through you, then
207
+the only way you could satisfy both it and this License would be to
208
+refrain entirely from distribution of the Program.
209
+
210
+If any portion of this section is held invalid or unenforceable under
211
+any particular circumstance, the balance of the section is intended to
212
+apply and the section as a whole is intended to apply in other
213
+circumstances.
214
+
215
+It is not the purpose of this section to induce you to infringe any
216
+patents or other property right claims or to contest validity of any
217
+such claims; this section has the sole purpose of protecting the
218
+integrity of the free software distribution system, which is
219
+implemented by public license practices.  Many people have made
220
+generous contributions to the wide range of software distributed
221
+through that system in reliance on consistent application of that
222
+system; it is up to the author/donor to decide if he or she is willing
223
+to distribute software through any other system and a licensee cannot
224
+impose that choice.
225
+
226
+This section is intended to make thoroughly clear what is believed to
227
+be a consequence of the rest of this License.
228
+
229
+  8. If the distribution and/or use of the Program is restricted in
230
+certain countries either by patents or by copyrighted interfaces, the
231
+original copyright holder who places the Program under this License
232
+may add an explicit geographical distribution limitation excluding
233
+those countries, so that distribution is permitted only in or among
234
+countries not thus excluded.  In such case, this License incorporates
235
+the limitation as if written in the body of this License.
236
+
237
+  9. The Free Software Foundation may publish revised and/or new versions
238
+of the General Public License from time to time.  Such new versions will
239
+be similar in spirit to the present version, but may differ in detail to
240
+address new problems or concerns.
241
+
242
+Each version is given a distinguishing version number.  If the Program
243
+specifies a version number of this License which applies to it and "any
244
+later version", you have the option of following the terms and conditions
245
+either of that version or of any later version published by the Free
246
+Software Foundation.  If the Program does not specify a version number of
247
+this License, you may choose any version ever published by the Free Software
248
+Foundation.
249
+
250
+  10. If you wish to incorporate parts of the Program into other free
251
+programs whose distribution conditions are different, write to the author
252
+to ask for permission.  For software which is copyrighted by the Free
253
+Software Foundation, write to the Free Software Foundation; we sometimes
254
+make exceptions for this.  Our decision will be guided by the two goals
255
+of preserving the free status of all derivatives of our free software and
256
+of promoting the sharing and reuse of software generally.
257
+
258
+                            NO WARRANTY
259
+
260
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
262
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
266
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
267
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+REPAIR OR CORRECTION.
269
+
270
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+POSSIBILITY OF SUCH DAMAGES.
279
+
280
+                     END OF TERMS AND CONDITIONS
281
+
282
+            How to Apply These Terms to Your New Programs
283
+
284
+  If you develop a new program, and you want it to be of the greatest
285
+possible use to the public, the best way to achieve this is to make it
286
+free software which everyone can redistribute and change under these terms.
287
+
288
+  To do so, attach the following notices to the program.  It is safest
289
+to attach them to the start of each source file to most effectively
290
+convey the exclusion of warranty; and each file should have at least
291
+the "copyright" line and a pointer to where the full notice is found.
292
+
293
+    <one line to give the program's name and a brief idea of what it does.>
294
+    Copyright (C) <year>  <name of author>
295
+
296
+    This program is free software; you can redistribute it and/or modify
297
+    it under the terms of the GNU General Public License as published by
298
+    the Free Software Foundation; either version 2 of the License, or
299
+    (at your option) any later version.
300
+
301
+    This program is distributed in the hope that it will be useful,
302
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
304
+    GNU General Public License for more details.
305
+
306
+    You should have received a copy of the GNU General Public License along
307
+    with this program; if not, write to the Free Software Foundation, Inc.,
308
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309
+
310
+Also add information on how to contact you by electronic and paper mail.
311
+
312
+If the program is interactive, make it output a short notice like this
313
+when it starts in an interactive mode:
314
+
315
+    Gnomovision version 69, Copyright (C) year name of author
316
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
+    This is free software, and you are welcome to redistribute it
318
+    under certain conditions; type `show c' for details.
319
+
320
+The hypothetical commands `show w' and `show c' should show the appropriate
321
+parts of the General Public License.  Of course, the commands you use may
322
+be called something other than `show w' and `show c'; they could even be
323
+mouse-clicks or menu items--whatever suits your program.
324
+
325
+You should also get your employer (if you work as a programmer) or your
326
+school, if any, to sign a "copyright disclaimer" for the program, if
327
+necessary.  Here is a sample; alter the names:
328
+
329
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
331
+
332
+  <signature of Ty Coon>, 1 April 1989
333
+  Ty Coon, President of Vice
334
+
335
+This General Public License does not permit incorporating your program into
336
+proprietary programs.  If your program is a subroutine library, you may
337
+consider it more useful to permit linking proprietary applications with the
338
+library.  If this is what you want to do, use the GNU Lesser General
339
+Public License instead of this License.

+ 44
- 0
Makefile Прегледај датотеку

@@ -0,0 +1,44 @@
1
+CC =		gcc
2
+CFLAGS =	-O2 -s -D_GNU_SOURCE
3
+LDLIBS =	-lcyassl -lm
4
+PREFIX =
5
+BINDIR =	$(PREFIX)/usr/bin
6
+
7
+ifeq ($(CROSS_BUILD), 1)
8
+	DEFS:=-DCROSS_BUILD
9
+else
10
+	DEFS:=
11
+endif
12
+
13
+all: webget webpost
14
+	@echo "All done!"
15
+
16
+webget: webget.o libweb.o
17
+	$(CC) $(CFLAGS) $(LDLIBS) webget.o libweb.o -o webget
18
+
19
+webpost: webpost.o libweb.o
20
+	$(CC) $(CFLAGS) $(LDLIBS) webpost.o libweb.o -o webpost
21
+
22
+webget.o: webget.c
23
+	$(CC) $(CFLAGS) $(DEFS) -c webget.c
24
+
25
+webpost.o: webpost.c
26
+	$(CC) $(CFLAGS) $(DEFS) -c webpost.c
27
+
28
+libweb.o: libweb.c
29
+	$(CC) $(CFLAGS) $(DEFS) -c libweb.c
30
+
31
+install:
32
+	mkdir -p $(BINDIR)
33
+	cp webget $(BINDIR)
34
+	cp webpost $(BINDIR)
35
+	@echo "All done!"
36
+
37
+uninstall:
38
+	@rm -f $(BINDIR)/webget
39
+	@rm -f $(BINDIR)/webpost
40
+	@echo "All done!"
41
+
42
+clean:
43
+	rm -f webget webpost *.o
44
+	@echo "All done!"

+ 21
- 0
README Прегледај датотеку

@@ -0,0 +1,21 @@
1
+WebGet
2
+======
3
+
4
+WebGet fetches an HTTP and HTTPS URLs and dumps the contents to stdout. It does
5
+not do gopher, ftp, file, news, or any other type of URL, only HTTP. It can be
6
+configured to do HTTPS fetches as well. 
7
+
8
+
9
+It is based on http_get <http://acme.com/software/http_get/>
10
+
11
+
12
+
13
+WebPost
14
+=======
15
+
16
+WebPost does a POST operation to an HTTP and HTTPS URLs and dumps the results to
17
+stdout. It does not do gopher, ftp, file, news, or any other type of URL, only
18
+HTTP. It can be configured to do HTTPS POSTs as well.
19
+
20
+
21
+It is based on http_post <http://acme.com/software/http_post/>

+ 0
- 4
README.md Прегледај датотеку

@@ -1,4 +0,0 @@
1
-webget
2
-======
3
-
4
-WebGet &amp; WebPost Utilities

+ 823
- 0
libweb.c Прегледај датотеку

@@ -0,0 +1,823 @@
1
+/**
2
+ * @PROJECT			WebGet Downloader
3
+ * @COPYRIGHT		See COPYING in the top level directory
4
+ * @FILE			libweb.c
5
+ * @PURPOSE			Common Web Library
6
+ * @DEVELOPERS		Al Globus <globus@nas.nasa.gov>
7
+ *					Rafal Kupiec <belliash@asiotec.eu.org>
8
+ *					Jef Poskanzer <jef@mail.acme.com>
9
+ */
10
+
11
+#include <unistd.h>
12
+#include <stdlib.h>
13
+#include <stdio.h>
14
+#include <string.h>
15
+#include <ctype.h>
16
+#include <signal.h>
17
+#include <fcntl.h>
18
+#include <sys/types.h>
19
+#include <sys/stat.h>
20
+#include <sys/socket.h>
21
+#include <netinet/in.h>
22
+#include <netdb.h>
23
+
24
+#include <cyassl/openssl/ssl.h>
25
+#include <cyassl/openssl/err.h>
26
+#include <cyassl/error.h> 
27
+
28
+#ifdef CROSS_BUILD
29
+	#include <cyassl/cyassl_error.h>
30
+#endif
31
+
32
+#include "libweb.h"
33
+
34
+int b64_encode(unsigned char* ptr, int len, char* space, int size) {
35
+	int ptr_idx, space_idx, phase;
36
+	char c;
37
+
38
+	space_idx = 0;
39
+	phase = 0;
40
+	for(ptr_idx = 0; ptr_idx < len; ++ptr_idx) {
41
+		switch(phase) {
42
+			case 0:
43
+				c = b64_encode_table[ptr[ptr_idx] >> 2];
44
+				if(space_idx < size) {
45
+					space[space_idx++] = c;
46
+				}
47
+				c = b64_encode_table[(ptr[ptr_idx] & 0x3) << 4];
48
+				if(space_idx < size) {
49
+					space[space_idx++] = c;
50
+				}
51
+				++phase;
52
+				break;
53
+			case 1:
54
+				space[space_idx - 1] = b64_encode_table[b64_decode_table[(int) ((unsigned char) space[space_idx - 1])] | (ptr[ptr_idx] >> 4)];
55
+				c = b64_encode_table[(ptr[ptr_idx] & 0xf) << 2];
56
+				if(space_idx < size) {
57
+					space[space_idx++] = c;
58
+				}
59
+				++phase;
60
+				break;
61
+			case 2:
62
+				space[space_idx - 1] = b64_encode_table[b64_decode_table[(int) ((unsigned char) space[space_idx - 1])] | (ptr[ptr_idx] >> 6)];
63
+				c = b64_encode_table[ptr[ptr_idx] & 0x3f];
64
+				if(space_idx < size) {
65
+					space[space_idx++] = c;
66
+				}
67
+				phase = 0;
68
+				break;
69
+		}
70
+	}
71
+	while(phase++ < 3) {
72
+		if(space_idx < size) {
73
+			space[space_idx++] = '=';
74
+		}
75
+	}
76
+	return space_idx;
77
+}
78
+
79
+void check(void* ptr) {
80
+	if(ptr == (void*) 0) {
81
+		(void) fprintf(stderr, "%s: out of memory\n", argv0);
82
+		exit(1);
83
+	}
84
+}
85
+
86
+off_t file_bytes(const char* filename) {
87
+	struct stat sb;
88
+
89
+	if(stat(filename, &sb) < 0) {
90
+		perror(filename);
91
+		exit(1);
92
+	}
93
+	return sb.st_size;
94
+}
95
+
96
+int file_copy(const char* filename, char* buf) {
97
+	int fd;
98
+	struct stat sb;
99
+	off_t bytes;
100
+
101
+	fd = open(filename, O_RDONLY);
102
+	if(fd == -1) {
103
+		perror(filename);
104
+		exit(-1);
105
+	}
106
+	if(fstat(fd, &sb) != 0) {
107
+		perror(filename);
108
+		exit(-1);
109
+	}
110
+	bytes = sb.st_size;
111
+	if(read(fd, buf, bytes) != bytes) {
112
+		perror(filename);
113
+		exit(-1);
114
+	}
115
+	(void) close(fd);
116
+	return bytes;
117
+}
118
+
119
+int getURL(char* url, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value) {
120
+	char* s;
121
+	int protocol;
122
+	char host[2000];
123
+	int host_len;
124
+	unsigned short port;
125
+	char* file = (char*) 0;
126
+	char* http = "http://";
127
+	int http_len = strlen( http );
128
+	char* https = "https://";
129
+	int https_len = strlen( https );
130
+	int proto_len;
131
+
132
+	if(url == (char*) 0) {
133
+		(void) fprintf(stderr, "%s: null URL\n", argv0);
134
+		exit(1);
135
+	}
136
+	if(strncmp(http, url, http_len) == 0) {
137
+		proto_len = http_len;
138
+		protocol = PROTO_HTTP;
139
+	} else if(strncmp(https, url, https_len) == 0) {
140
+		proto_len = https_len;
141
+		protocol = PROTO_HTTPS;
142
+	} else {
143
+		(void) fprintf(stderr, "%s: non-http URL\n", argv0);
144
+		exit(1);
145
+	}
146
+
147
+	for(s = url + proto_len; *s != '\0' && *s != ':' && *s != '/'; ++s);
148
+	host_len = s - url;
149
+	host_len -= proto_len;
150
+	strncpy(host, url + proto_len, host_len);
151
+	host[host_len] = '\0';
152
+	if(*s == ':') {
153
+		port = (unsigned short) atoi(++s);
154
+		while(*s != '\0' && *s != '/')
155
+		++s;
156
+	} else {
157
+		if(protocol == PROTO_HTTPS) {
158
+			port = 443;
159
+		} else {
160
+			port = 80;
161
+		}
162
+	}
163
+	if(*s == '\0') {
164
+		file = "/";
165
+	} else {
166
+		file = s;
167
+	}
168
+	return getURLbyParts(protocol, host, port, file, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value);
169
+}
170
+
171
+int getURLbyParts(int protocol, char* host, unsigned short port, char* file, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value) {
172
+	int sockfd;
173
+	SSL_CTX* ssl_ctx = (SSL_CTX*) 0;
174
+	SSL* ssl = (SSL*) 0;
175
+	char buf[20000];
176
+	int i, bytes, b, header_state, status;
177
+
178
+	(void) alarm(timeout);
179
+	sockfd = open_client_socket(host, port);
180
+	if(protocol == PROTO_HTTPS) {
181
+		int r;
182
+		ssl_ctx = SSL_CTX_new(TLSv1_client_method());
183
+		SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, 0); 
184
+		ssl = SSL_new(ssl_ctx);
185
+		SSL_set_fd(ssl, sockfd);
186
+		r = SSL_connect(ssl);
187
+		if(r <= 0) {
188
+			(void) fprintf(stderr, "%s: %s - SSL connection failed - %d\n", argv0, url, r);
189
+			exit(1);
190
+		}
191
+	}
192
+	(void) alarm(timeout);
193
+	bytes = snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n", file);
194
+	bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Host: %s\r\n", host);
195
+	if(referer != (char*) 0) {
196
+		bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Referer: %s\r\n", referer);
197
+	}
198
+	bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "User-Agent: %s\r\n", user_agent);
199
+	bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept: */*\r\n");
200
+	bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept-Language: en\r\n");
201
+	bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept-Charset: iso-8859-1,*,utf-8\r\n");
202
+	if(auth_token != (char*) 0) {
203
+		char token_buf[1000];
204
+		token_buf[b64_encode((unsigned char*) auth_token, strlen( auth_token ), token_buf, sizeof(token_buf))] = '\0';
205
+		bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Authorization: Basic %s\r\n", token_buf);
206
+	}
207
+	for(i = 0; i < ncookies; ++i) {
208
+		bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Cookie: %s\r\n", cookies[i]);
209
+	}
210
+	if(header_name != (char*) 0) {
211
+		bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "%s: %s\r\n", header_name, header_value);
212
+	}
213
+	bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "\r\n");
214
+	if (protocol == PROTO_HTTPS)
215
+		(void) SSL_write(ssl, buf, bytes);
216
+	else
217
+		(void) write(sockfd, buf, bytes);
218
+	(void) alarm(timeout);
219
+	header_state = HDST_LINE1_PROTOCOL;
220
+	status = 0;
221
+	for(;;) {
222
+		if(protocol == PROTO_HTTPS) {
223
+			bytes = SSL_read(ssl, buf, sizeof(buf));
224
+		} else {
225
+			bytes = read(sockfd, buf, sizeof(buf));
226
+		}
227
+		if(bytes <= 0) {
228
+			break;
229
+		}
230
+		for(b = 0; b < bytes; ++b) {
231
+			if(verbose) {
232
+				(void) write(1, &buf[b], 1);
233
+			}
234
+			switch(header_state) {
235
+				case HDST_LINE1_PROTOCOL:
236
+					switch(buf[b]) {
237
+						case ' ': case '\t':
238
+							header_state = HDST_LINE1_WHITESPACE;
239
+							break;
240
+						case '\n':
241
+							header_state = HDST_LF;
242
+							break;
243
+						case '\r':
244
+							header_state = HDST_CR;
245
+							break;
246
+					}
247
+					break;
248
+				case HDST_LINE1_WHITESPACE:
249
+					switch(buf[b]) {
250
+						case '0':
251
+						case '1':
252
+						case '2':
253
+						case '3':
254
+						case '4':
255
+						case '5':
256
+						case '6':
257
+						case '7':
258
+						case '8':
259
+						case '9':
260
+							status = buf[b] - '0';
261
+							header_state = HDST_LINE1_STATUS;
262
+							break;
263
+						case '\n':
264
+							header_state = HDST_LF;
265
+							break;
266
+						case '\r':
267
+							header_state = HDST_CR;
268
+							break;
269
+						default:
270
+							header_state = HDST_TEXT;
271
+							break;
272
+					}
273
+					break;
274
+				case HDST_LINE1_STATUS:
275
+					switch(buf[b]) {
276
+						case '0':
277
+						case '1':
278
+						case '2':
279
+						case '3':
280
+						case '4':
281
+						case '5':
282
+						case '6':
283
+						case '7':
284
+						case '8':
285
+						case '9':
286
+							status = status * 10 + buf[b] - '0';
287
+							break;
288
+						case '\n':
289
+							header_state = HDST_LF;
290
+							break;
291
+						case '\r':
292
+							header_state = HDST_CR;
293
+							break;
294
+						default:
295
+							header_state = HDST_TEXT;
296
+							break;
297
+					}
298
+					break;
299
+				case HDST_BOL:
300
+					switch(buf[b]) {
301
+						case '\n':
302
+							header_state = HDST_LF;
303
+							break;
304
+						case '\r':
305
+							header_state = HDST_CR;
306
+							break;
307
+						default:
308
+							header_state = HDST_TEXT;
309
+							break;
310
+					}
311
+					break;
312
+				case HDST_TEXT:
313
+					switch(buf[b]) {
314
+						case '\n':
315
+							header_state = HDST_LF;
316
+							break;
317
+						case '\r':
318
+							header_state = HDST_CR;
319
+							break;
320
+					}
321
+					break;
322
+				case HDST_LF:
323
+					switch(buf[b]) {
324
+						case '\n':
325
+							goto end_of_headers;
326
+						case '\r':
327
+							header_state = HDST_CR;
328
+							break;
329
+						default:
330
+							header_state = HDST_TEXT;
331
+							break;
332
+					}
333
+					break;
334
+				case HDST_CR:
335
+					switch(buf[b]) {
336
+						case '\n':
337
+							header_state = HDST_CRLF;
338
+							break;
339
+						case '\r':
340
+							goto end_of_headers;
341
+						default:
342
+							header_state = HDST_TEXT;
343
+							break;
344
+					}
345
+					break;
346
+				case HDST_CRLF:
347
+					switch(buf[b]) {
348
+						case '\n':
349
+							goto end_of_headers;
350
+						case '\r':
351
+							header_state = HDST_CRLFCR;
352
+							break;
353
+						default:
354
+							header_state = HDST_TEXT;
355
+							break;
356
+					}
357
+					break;
358
+				case HDST_CRLFCR:
359
+					switch(buf[b]) {
360
+						case '\n':
361
+						case '\r':
362
+							goto end_of_headers;
363
+						default:
364
+							header_state = HDST_TEXT;
365
+							break;
366
+					}
367
+					break;
368
+			}
369
+		}
370
+	}
371
+end_of_headers:
372
+	if(bytes > 0) {
373
+		++b;
374
+		(void) write(1, &buf[b], bytes - b);
375
+	}
376
+	for(;;) {
377
+		(void) alarm(timeout);
378
+		if(protocol == PROTO_HTTPS) {
379
+			bytes = SSL_read(ssl, buf, sizeof(buf));
380
+		} else {
381
+			bytes = read(sockfd, buf, sizeof(buf));
382
+		}
383
+		if(bytes == 0) {
384
+			break;
385
+		}
386
+		if(bytes < 0) {
387
+			show_error("read");
388
+		}
389
+		(void) write(1, buf, bytes);
390
+	}
391
+	if(protocol == PROTO_HTTPS) {
392
+		SSL_free(ssl);
393
+		SSL_CTX_free(ssl_ctx);
394
+	}
395
+	(void) close(sockfd);
396
+	return status;
397
+}
398
+
399
+void* malloc_check(size_t size) {
400
+	void* ptr = malloc(size);
401
+	check(ptr);
402
+	return ptr;
403
+}
404
+
405
+int open_client_socket(char* hostname, unsigned short port) {
406
+	struct addrinfo hints;
407
+	char portstr[10];
408
+	int gaierr;
409
+	struct addrinfo* ai;
410
+	struct addrinfo* ai2;
411
+	struct addrinfo* aiv4;
412
+	struct addrinfo* aiv6;
413
+	struct sockaddr_in6 sa;
414
+	int sa_len, sock_family, sock_type, sock_protocol;
415
+	int sockfd;
416
+
417
+	(void) memset((void*) &sa, 0, sizeof(sa));
418
+	(void) memset(&hints, 0, sizeof(hints));
419
+	hints.ai_family = PF_UNSPEC;
420
+	hints.ai_socktype = SOCK_STREAM;
421
+	(void) snprintf(portstr, sizeof(portstr), "%d", (int) port);
422
+	if((gaierr = getaddrinfo(hostname, portstr, &hints, &ai)) != 0) {
423
+		(void) fprintf(stderr, "%s: getaddrinfo %s - %s\n", argv0, hostname, gai_strerror(gaierr));
424
+		exit(1);
425
+	}
426
+	aiv4 = (struct addrinfo*) 0;
427
+	aiv6 = (struct addrinfo*) 0;
428
+	for(ai2 = ai; ai2 != (struct addrinfo*) 0; ai2 = ai2->ai_next) {
429
+		switch(ai2->ai_family) {
430
+			case AF_INET: 
431
+				if(aiv4 == (struct addrinfo*) 0) {
432
+					aiv4 = ai2;
433
+				}
434
+				break;
435
+			case AF_INET6:
436
+				if(aiv6 == (struct addrinfo*) 0) {
437
+					aiv6 = ai2;
438
+				}
439
+				break;
440
+		}
441
+	}
442
+	if(aiv4 != (struct addrinfo*) 0) {
443
+		if(sizeof(sa) < aiv4->ai_addrlen) {
444
+			(void) fprintf(stderr, "%s - sockaddr too small (%lu < %lu)\n", hostname, (unsigned long) sizeof(sa), (unsigned long) aiv4->ai_addrlen);
445
+			exit(1);
446
+		}
447
+		sock_family = aiv4->ai_family;
448
+		sock_type = aiv4->ai_socktype;
449
+		sock_protocol = aiv4->ai_protocol;
450
+		sa_len = aiv4->ai_addrlen;
451
+		(void) memmove(&sa, aiv4->ai_addr, sa_len);
452
+		goto ok;
453
+	}
454
+	if(aiv6 != (struct addrinfo*) 0) {
455
+		if(sizeof(sa) < aiv6->ai_addrlen) {
456
+			(void) fprintf(stderr, "%s - sockaddr too small (%lu < %lu)\n", hostname, (unsigned long) sizeof(sa), (unsigned long) aiv6->ai_addrlen);
457
+			exit(1);
458
+		}
459
+		sock_family = aiv6->ai_family;
460
+		sock_type = aiv6->ai_socktype;
461
+		sock_protocol = aiv6->ai_protocol;
462
+		sa_len = aiv6->ai_addrlen;
463
+		(void) memmove(&sa, aiv6->ai_addr, sa_len);
464
+		goto ok;
465
+	}
466
+	(void) fprintf(stderr, "%s: no valid address found for host %s\n", argv0, hostname);
467
+	exit(1);
468
+ok:
469
+	freeaddrinfo(ai);
470
+	sockfd = socket(sock_family, sock_type, sock_protocol);
471
+	if(sockfd < 0) {
472
+		show_error("socket");
473
+	}
474
+	if(connect(sockfd, (struct sockaddr*) &sa, sa_len) < 0) {
475
+		show_error("connect");
476
+	}
477
+	return sockfd;
478
+}
479
+
480
+void postURL(char* url, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value, char** args, int argc) {
481
+	char* s;
482
+	int protocol;
483
+	char host[2000];
484
+	int host_len;
485
+	unsigned short port;
486
+	char* file = 0;
487
+	char* http = "http://";
488
+	int http_len = strlen(http);
489
+	char* https = "https://";
490
+	int https_len = strlen(https);
491
+	int proto_len;
492
+
493
+	if(url == (char*) 0) {
494
+		(void) fprintf(stderr, "%s: null URL\n", argv0);
495
+		exit(1);
496
+	}
497
+	if(strncmp(http, url, http_len) == 0) {
498
+		proto_len = http_len;
499
+		protocol = PROTO_HTTP;
500
+	} else if(strncmp(https, url, https_len) == 0) {
501
+		proto_len = https_len;
502
+		protocol = PROTO_HTTPS;
503
+	} else {
504
+		(void) fprintf(stderr, "%s: non-http URL\n", argv0);
505
+		exit(1);
506
+	}
507
+	for(s = url + proto_len; *s != '\0' && *s != ':' && *s != '/'; ++s);
508
+	host_len = s - url;
509
+	host_len -= proto_len;
510
+	strncpy(host, url + proto_len, host_len);
511
+	host[host_len] = '\0';
512
+	if(*s == ':') {
513
+		port = (unsigned short) atoi(++s);
514
+		while(*s != '\0' && *s != '/') {
515
+			++s;
516
+		}
517
+	} else {
518
+		if(protocol == PROTO_HTTPS) {
519
+			port = 443;
520
+		} else {
521
+			port = 80;
522
+		}
523
+	}
524
+	if(*s == '\0') {
525
+		file = "/";
526
+	} else {
527
+		file = s;
528
+	}
529
+	postURLbyParts(protocol, host, port, file, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value, args, argc);
530
+}
531
+
532
+void postURLbyParts(int protocol, char* host, unsigned short port, char* file, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value, char** args, int argc) {
533
+	int sockfd;
534
+	SSL_CTX* ssl_ctx = (SSL_CTX*) 0;
535
+	SSL* ssl = (SSL*) 0;
536
+	char head_buf[20000];
537
+	int max_arg, total_bytes;
538
+	int multipart, next_arg_is_file;
539
+	static const char* const sep = "http_post-content-separator";
540
+	char* data_buf;
541
+	char* enc_buf;
542
+	int head_bytes, data_bytes, i, header_state;
543
+	char* eq;
544
+
545
+	(void) alarm(timeout);
546
+	sockfd = open_client_socket(host, port);
547
+	if(protocol == PROTO_HTTPS) {
548
+		int r;
549
+		ssl_ctx = SSL_CTX_new(TLSv1_client_method());
550
+		SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, 0); 
551
+		ssl = SSL_new(ssl_ctx);
552
+		SSL_set_fd(ssl, sockfd);
553
+		r = SSL_connect(ssl);
554
+		if(r <= 0) {
555
+			(void) fprintf(stderr, "%s: %s - SSL connection failed - %d\n", argv0, url, r);
556
+			exit(1);
557
+		}
558
+	}
559
+	multipart = 0;
560
+	total_bytes = max_arg = 0;
561
+	next_arg_is_file = 0;
562
+	for(i = 0; i < argc ; ++i) {
563
+		int l = strlen(args[i]);
564
+		if(strcmp(args[i], "-f") == 0) {
565
+			multipart = 1;
566
+			next_arg_is_file = 1;
567
+			continue;
568
+		}
569
+		total_bytes += l;
570
+		if(l > max_arg) {
571
+			max_arg = l;
572
+		}
573
+		if(next_arg_is_file) {
574
+			eq = strchr(args[i], '=');
575
+			if(eq == (char*) 0) {
576
+				(void) fprintf(stderr, "%s: missing filename\n", argv0);
577
+				exit(1);
578
+			} else {
579
+				++eq;
580
+				total_bytes += file_bytes(eq);
581
+			}
582
+			next_arg_is_file = 0;
583
+		}
584
+	}
585
+	if(multipart) {
586
+		for(i = 0; i < argc ; ++i) {
587
+			total_bytes += strlen(sep) * 2 + 100;
588
+		}
589
+		total_bytes += strlen(sep) * 2;
590
+	} else {
591
+		total_bytes *= 4;
592
+		enc_buf = (char*) malloc_check(max_arg * 4);
593
+	}
594
+	data_buf = (char*) malloc_check(total_bytes);
595
+	if(multipart) {
596
+		next_arg_is_file = 0;
597
+		data_bytes = 0;
598
+		for(i = 0; i < argc; ++i) {
599
+			if(strcmp(args[i], "-f") == 0) {
600
+				next_arg_is_file = 1;
601
+				continue;
602
+			}
603
+			eq = strchr(args[i], '=');
604
+			if(eq == (char*) 0) {
605
+				data_bytes += sprintf(&data_buf[data_bytes], "--%s\r\nContent-Disposition: form-data\r\n\r\n%s\r\n", sep, args[i]);
606
+			} else {
607
+				*eq++ = '\0';
608
+				if(next_arg_is_file) {
609
+					data_bytes += sprintf(&data_buf[data_bytes], "--%s\r\nContent-Disposition: form-data; name=\"%s\"; filename=\"%s\"\r\n\r\n", sep, args[i], eq);
610
+					data_bytes += file_copy(eq, &data_buf[data_bytes]);
611
+					data_bytes += sprintf(&data_buf[data_bytes], "\r\n");
612
+					next_arg_is_file = 0;
613
+				} else {
614
+					data_bytes += sprintf(&data_buf[data_bytes], "--%s\r\nContent-Disposition: form-data; name=\"%s\"\r\n\r\n%s\r\n", sep, args[i], eq);
615
+				}
616
+			}
617
+		}
618
+		data_bytes += sprintf(&data_buf[data_bytes], "--%s--\r\n", sep);
619
+	} else {
620
+		data_bytes = 0;
621
+		for(i = 0; i < argc ; ++i) {
622
+			if(data_bytes > 0) {
623
+				data_bytes += sprintf(&data_buf[data_bytes], "&");
624
+			}
625
+			eq = strchr(args[i], '=');
626
+			if(eq == (char*) 0) {
627
+				strencode(enc_buf, args[i]);
628
+				data_bytes += sprintf(&data_buf[data_bytes], "%s", enc_buf);
629
+			} else {
630
+				*eq++ = '\0';
631
+				strencode(enc_buf, args[i]);
632
+				data_bytes += sprintf(&data_buf[data_bytes], "%s=", enc_buf);
633
+				strencode(enc_buf, eq);
634
+				data_bytes += sprintf(&data_buf[data_bytes], "%s", enc_buf);
635
+			}
636
+		}
637
+	}
638
+	(void) alarm(timeout);
639
+	head_bytes = snprintf(head_buf, sizeof(head_buf), "POST %s HTTP/1.0\r\n", file);
640
+	head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Host: %s\r\n", host);
641
+	head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-Length: %d\r\n", data_bytes);
642
+	if(referer != (char*) 0) {
643
+		head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Referer: %s\r\n", referer);
644
+	}
645
+	head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "User-Agent: %s\r\n", user_agent);
646
+	if(multipart) {
647
+		head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-type: multipart/form-data; boundary=\"%s\"\r\n", sep);
648
+	} else {
649
+		head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-type: application/x-www-form-urlencoded\r\n");
650
+	}
651
+	head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept: */*\r\n");
652
+	head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept-Language: en\r\n");
653
+	head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept-Charset: iso-8859-1,*,utf-8\r\n");
654
+	if(auth_token != (char*) 0) {
655
+		char token_buf[1000];
656
+		token_buf[b64_encode((unsigned char*) auth_token, strlen(auth_token), token_buf, sizeof(token_buf))] = '\0';
657
+		head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Authorization: Basic %s\r\n", token_buf);
658
+	}
659
+	for(i = 0; i < ncookies; ++i) {
660
+		head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Cookie: %s\r\n", cookies[i]);
661
+	}
662
+	if(header_name != (char*) 0) {
663
+		head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "%s: %s\r\n", header_name, header_value);
664
+	}
665
+	head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "\r\n");
666
+	if(protocol == PROTO_HTTPS) {
667
+		(void) SSL_write(ssl, head_buf, head_bytes);
668
+	} else {
669
+		(void) write(sockfd, head_buf, head_bytes);
670
+	}
671
+	if(protocol == PROTO_HTTPS) {
672
+		(void) SSL_write(ssl, data_buf, data_bytes);
673
+	} else {
674
+		(void) write(sockfd, data_buf, data_bytes);
675
+	}
676
+	(void) alarm(timeout);
677
+	header_state = HDST_BOL;
678
+	for(;;) {
679
+		if(protocol == PROTO_HTTPS) {
680
+			head_bytes = SSL_read(ssl, head_buf, sizeof(head_buf));
681
+		} else {
682
+			head_bytes = read(sockfd, head_buf, sizeof(head_buf));
683
+		}
684
+		if(head_bytes <= 0) {
685
+			break;
686
+		}
687
+		for(i = 0; i < head_bytes; ++i) {
688
+			if(verbose) {
689
+				(void) write(1, &head_buf[i], 1);
690
+			}
691
+			switch(header_state) {
692
+				case HDST_BOL:
693
+					switch(head_buf[i]) {
694
+						case '\n':
695
+							header_state = HDST_LF;
696
+							break;
697
+						case '\r':
698
+							header_state = HDST_CR;
699
+							break;
700
+						default:
701
+							header_state = HDST_TEXT;
702
+							break;
703
+					}
704
+					break;
705
+				case HDST_TEXT:
706
+					switch(head_buf[i]) {
707
+						case '\n':
708
+							header_state = HDST_LF;
709
+							break;
710
+						case '\r':
711
+							header_state = HDST_CR;
712
+							break;
713
+					}
714
+					break;
715
+				case HDST_LF:
716
+					switch(head_buf[i]) {
717
+						case '\n':
718
+							goto end_of_headers;
719
+						case '\r':
720
+							header_state = HDST_CR;
721
+							break;
722
+						default:
723
+							header_state = HDST_TEXT;
724
+							break;
725
+					}
726
+					break;
727
+				case HDST_CR:
728
+					switch(head_buf[i]) {
729
+						case '\n':
730
+							header_state = HDST_CRLF;
731
+							break;
732
+						case '\r':
733
+							goto end_of_headers;
734
+						default:
735
+							header_state = HDST_TEXT;
736
+							break;
737
+					}
738
+					break;
739
+				case HDST_CRLF:
740
+					switch(head_buf[i]) {
741
+						case '\n':
742
+							goto end_of_headers;
743
+						case '\r':
744
+							header_state = HDST_CRLFCR;
745
+							break;
746
+						default:
747
+							header_state = HDST_TEXT;
748
+							break;
749
+					}
750
+					break;
751
+				case HDST_CRLFCR:
752
+					switch(head_buf[i]) {
753
+						case '\n':
754
+						case '\r':
755
+							goto end_of_headers;
756
+						default:
757
+							header_state = HDST_TEXT;
758
+							break;
759
+					}
760
+					break;
761
+			}
762
+		}
763
+	}
764
+end_of_headers:
765
+	if(head_bytes > 0) {
766
+		++i;
767
+		(void) write(1, &head_buf[i], head_bytes - i);
768
+	}
769
+	for(;;) {
770
+		(void) alarm(timeout);
771
+		if(protocol == PROTO_HTTPS) {
772
+			head_bytes = SSL_read(ssl, head_buf, sizeof(head_buf));
773
+		} else {
774
+			head_bytes = read(sockfd, head_buf, sizeof(head_buf));
775
+		}
776
+		if(head_bytes == 0) {
777
+			break;
778
+		}
779
+		if(head_bytes < 0) {
780
+			show_error("read");
781
+		}
782
+		(void) write(1, head_buf, head_bytes);
783
+	}
784
+	if(protocol == PROTO_HTTPS) {
785
+		SSL_free(ssl);
786
+		SSL_CTX_free(ssl_ctx);
787
+	}
788
+	(void) close(sockfd);
789
+}
790
+
791
+void show_error(char* cause) {
792
+	char buf[5000];
793
+	(void) sprintf(buf, "%s: %s - %s", argv0, url, cause);
794
+	perror(buf);
795
+	exit(1);
796
+}
797
+
798
+void sigcatch(int sig) {
799
+	(void) fprintf(stderr, "%s: %s - timed out\n", argv0, url);
800
+	exit(1);
801
+}
802
+
803
+void strencode(char* to, char* from) {
804
+	int tolen;
805
+
806
+	for(tolen = 0; *from != '\0'; ++from) {
807
+		if(isalnum(*from) || strchr("/_.", *from) != (char*) 0) {
808
+			*to = *from;
809
+			++to;
810
+			++tolen;
811
+		} else {
812
+			(void) sprintf(to, "%%%02x", (int) *from & 0xff);
813
+			to += 3;
814
+			tolen += 3;
815
+		}
816
+	}
817
+	*to = '\0';
818
+}
819
+
820
+void usage() {
821
+	(void) fprintf(stderr, "usage:  %s [-c cookie] [-t timeout] [-r referer] [-u user-agent] [-a username:password] [-h header value] [-v] url\n", argv0);
822
+	exit(1);
823
+}

+ 79
- 0
libweb.h Прегледај датотеку

@@ -0,0 +1,79 @@
1
+/**
2
+ * @PROJECT			WebGet Downloader
3
+ * @COPYRIGHT		See COPYING in the top level directory
4
+ * @FILE			libweb.h
5
+ * @PURPOSE			Common Web Library
6
+ * @DEVELOPERS		Al Globus <globus@nas.nasa.gov>
7
+ *					Rafal Kupiec <belliash@asiotec.eu.org>
8
+ *					Jef Poskanzer <jef@mail.acme.com>
9
+ */
10
+
11
+#ifndef __LIBWEB_H
12
+#define __LIBWEB_H
13
+
14
+#define HDST_LINE1_PROTOCOL 0
15
+#define HDST_LINE1_WHITESPACE 1
16
+#define HDST_LINE1_STATUS 2
17
+#define HDST_BOL 10
18
+#define HDST_TEXT 11
19
+#define HDST_LF 12
20
+#define HDST_CR 13
21
+#define HDST_CRLF 14
22
+#define HDST_CRLFCR 15
23
+
24
+#define PROTO_HTTP 0
25
+#define PROTO_HTTPS 1
26
+
27
+#define MAX_COOKIES 20
28
+
29
+char* argv0;
30
+int timeout;
31
+char* url;
32
+int verbose;
33
+
34
+static int b64_decode_table[256] = {
35
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 00-0F */
36
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 10-1F */
37
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,  /* 20-2F */
38
+	52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,  /* 30-3F */
39
+	-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,  /* 40-4F */
40
+	15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,  /* 50-5F */
41
+	-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,  /* 60-6F */
42
+	41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,  /* 70-7F */
43
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 80-8F */
44
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* 90-9F */
45
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* A0-AF */
46
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* B0-BF */
47
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* C0-CF */
48
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* D0-DF */
49
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,  /* E0-EF */
50
+	-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1   /* F0-FF */
51
+};
52
+
53
+static char b64_encode_table[64] = {
54
+	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',  /* 0-7 */
55
+	'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',  /* 8-15 */
56
+	'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',  /* 16-23 */
57
+	'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',  /* 24-31 */
58
+	'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',  /* 32-39 */
59
+	'o', 'p', 'q', 'r', 's', 't', 'u', 'v',  /* 40-47 */
60
+	'w', 'x', 'y', 'z', '0', '1', '2', '3',  /* 48-55 */
61
+	'4', '5', '6', '7', '8', '9', '+', '/'   /* 56-63 */
62
+};
63
+            
64
+int b64_encode(unsigned char* ptr, int len, char* space, int size);
65
+void check(void* ptr);
66
+off_t file_bytes(const char* filename);
67
+int file_copy(const char* filename, char* buf);
68
+int getURL(char* url, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value);
69
+int getURLbyParts(int protocol, char* host, unsigned short port, char* file, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value);
70
+void* malloc_check(size_t size);
71
+int open_client_socket(char* hostname, unsigned short port);
72
+void postURL(char* url, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value, char** args, int argc);
73
+void postURLbyParts(int protocol, char* host, unsigned short port, char* file, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value, char** args, int argc);
74
+void show_error(char* cause);
75
+void sigcatch(int sig);
76
+void strencode(char* to, char* from);
77
+void usage();
78
+
79
+#endif

+ 89
- 0
webget.c Прегледај датотеку

@@ -0,0 +1,89 @@
1
+/**
2
+ * @PROJECT			WebGet Downloader
3
+ * @COPYRIGHT		See COPYING in the top level directory
4
+ * @FILE			webget.c
5
+ * @PURPOSE			Web To StdOut Downloader
6
+ * @DEVELOPERS		Al Globus <globus@nas.nasa.gov>
7
+ *					Rafal Kupiec <belliash@asiotec.eu.org>
8
+ *					Jef Poskanzer <jef@mail.acme.com>
9
+ */
10
+
11
+#include <stdio.h>
12
+#include <stdlib.h>
13
+#include <signal.h>
14
+
15
+#include "libweb.h"
16
+
17
+int main(int argc, char** argv) {
18
+	int argn;
19
+	char* referer;
20
+	char* user_agent;
21
+	char* auth_token;
22
+	int ncookies;
23
+	char* cookies[MAX_COOKIES];
24
+	char* header_name;
25
+	char* header_value;
26
+	int status;
27
+
28
+	argv0 = argv[0];
29
+	argn = 1;
30
+	timeout = 60;
31
+	referer = (char*) 0;
32
+	user_agent = "http_get";
33
+	auth_token = (char*) 0;
34
+	ncookies = 0;
35
+	header_name = (char*) 0;
36
+	header_value = (char*) 0;
37
+	verbose = 0;
38
+
39
+	while(argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
40
+		if(strcmp(argv[argn], "-t") == 0 && argn + 1 < argc) {
41
+			++argn;
42
+			timeout = atoi(argv[argn]);
43
+		} else if(strcmp(argv[argn], "-r") == 0 && argn + 1 < argc) {
44
+			++argn;
45
+			referer = argv[argn];
46
+		} else if(strcmp(argv[argn], "-u") == 0 && argn + 1 < argc) {
47
+			++argn;
48
+			user_agent = argv[argn];
49
+		} else if(strcmp(argv[argn], "-a") == 0 && argn + 1 < argc) {
50
+			++argn;
51
+			auth_token = argv[argn];
52
+		} else if(strcmp(argv[argn], "-c") == 0 && argn + 1 < argc) {
53
+			if(ncookies >= MAX_COOKIES) {
54
+				(void) fprintf(stderr, "%s: too many cookies\n", argv0);
55
+				exit (1);
56
+			}
57
+			++argn;
58
+			cookies[ncookies++] = argv[argn];
59
+		} else if(strcmp(argv[argn], "-h") == 0 && argn + 2 < argc) {
60
+			++argn;
61
+			header_name = argv[argn];
62
+			++argn;
63
+			header_value = argv[argn];
64
+		} else if(strcmp(argv[argn], "-v") == 0) {
65
+			verbose = 1;
66
+		} else {
67
+			usage();
68
+		}
69
+		++argn;
70
+	}
71
+	if(argn >= argc) {
72
+		usage();
73
+	}
74
+	url = argv[argn];
75
+	++argn;
76
+	if(argn != argc) {
77
+		usage();
78
+	}
79
+
80
+	(void) signal(SIGALRM, sigcatch);
81
+	status = getURL(url, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value);
82
+	if(status == 200) {
83
+		exit(0);
84
+	} else if(status == 0) {
85
+		exit(1);
86
+	} else {
87
+		exit(status);
88
+	}
89
+}

+ 79
- 0
webpost.c Прегледај датотеку

@@ -0,0 +1,79 @@
1
+/**
2
+ * @PROJECT			WebGet Downloader
3
+ * @COPYRIGHT		See COPYING in the top level directory
4
+ * @FILE			webpost.c
5
+ * @PURPOSE			Web POST Operation Dumper
6
+ * @DEVELOPERS		Al Globus <globus@nas.nasa.gov>
7
+ *					Rafal Kupiec <belliash@asiotec.eu.org>
8
+ *					Jef Poskanzer <jef@mail.acme.com>
9
+ */
10
+
11
+#include <stdio.h>
12
+#include <stdlib.h>
13
+#include <signal.h>
14
+
15
+#include "libweb.h"
16
+
17
+int main(int argc, char** argv) {
18
+	int argn;
19
+	char* referer;
20
+	char* user_agent;
21
+	char* auth_token;
22
+	int ncookies;
23
+	char* cookies[MAX_COOKIES];
24
+	char* header_name;
25
+	char* header_value;
26
+
27
+	argv0 = argv[0];
28
+	argn = 1;
29
+	timeout = 60;
30
+	referer = (char*) 0;
31
+	user_agent = "http_post";
32
+	auth_token = (char*) 0;
33
+	ncookies = 0;
34
+	header_name = (char*) 0;
35
+	header_value = (char*) 0;
36
+	verbose = 0;
37
+
38
+	while(argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
39
+		if(strcmp(argv[argn], "-t") == 0 && argn + 1 < argc) {
40
+			++argn;
41
+			timeout = atoi(argv[argn]);
42
+		} else if(strcmp(argv[argn], "-r") == 0 && argn + 1 < argc) {
43
+			++argn;
44
+			referer = argv[argn];
45
+		} else if(strcmp(argv[argn], "-u") == 0 && argn + 1 < argc) {
46
+			++argn;
47
+			user_agent = argv[argn];
48
+		} else if(strcmp(argv[argn], "-a") == 0 && argn + 1 < argc) {
49
+			++argn;
50
+			auth_token = argv[argn];
51
+		} else if(strcmp(argv[argn], "-c") == 0 && argn + 1 < argc) {
52
+			if(ncookies >= MAX_COOKIES) {
53
+				(void) fprintf(stderr, "%s: too many cookies\n", argv0);
54
+				exit(1);
55
+			}
56
+			++argn;
57
+			cookies[ncookies++] = argv[argn];
58
+		} else if(strcmp(argv[argn], "-h") == 0 && argn + 2 < argc) {
59
+			++argn;
60
+			header_name = argv[argn];
61
+			++argn;
62
+			header_value = argv[argn];
63
+		} else if(strcmp(argv[argn], "-v") == 0) {
64
+			verbose = 1;
65
+		} else {
66
+			usage();
67
+		}
68
+		++argn;
69
+	}
70
+	if(argn >= argc) {
71
+		usage();
72
+	}
73
+	url = argv[argn];
74
+	++argn;
75
+
76
+	(void) signal(SIGALRM, sigcatch);
77
+	postURL(url, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value, &(argv[argn]), argc - argn);
78
+	exit(0);
79
+}

Loading…
Откажи
Сачувај