Initial import
This commit is contained in:
parent
e7b12fd699
commit
d79b294105
339
COPYING
Normal file
339
COPYING
Normal file
@ -0,0 +1,339 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 2, June 1991
|
||||
|
||||
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The licenses for most software are designed to take away your
|
||||
freedom to share and change it. By contrast, the GNU General Public
|
||||
License is intended to guarantee your freedom to share and change free
|
||||
software--to make sure the software is free for all its users. This
|
||||
General Public License applies to most of the Free Software
|
||||
Foundation's software and to any other program whose authors commit to
|
||||
using it. (Some other Free Software Foundation software is covered by
|
||||
the GNU Lesser General Public License instead.) You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
this service if you wish), that you receive source code or can get it
|
||||
if you want it, that you can change the software or use pieces of it
|
||||
in new free programs; and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to make restrictions that forbid
|
||||
anyone to deny you these rights or to ask you to surrender the rights.
|
||||
These restrictions translate to certain responsibilities for you if you
|
||||
distribute copies of the software, or if you modify it.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must give the recipients all the rights that
|
||||
you have. You must make sure that they, too, receive or can get the
|
||||
source code. And you must show them these terms so they know their
|
||||
rights.
|
||||
|
||||
We protect your rights with two steps: (1) copyright the software, and
|
||||
(2) offer you this license which gives you legal permission to copy,
|
||||
distribute and/or modify the software.
|
||||
|
||||
Also, for each author's protection and ours, we want to make certain
|
||||
that everyone understands that there is no warranty for this free
|
||||
software. If the software is modified by someone else and passed on, we
|
||||
want its recipients to know that what they have is not the original, so
|
||||
that any problems introduced by others will not reflect on the original
|
||||
authors' reputations.
|
||||
|
||||
Finally, any free program is threatened constantly by software
|
||||
patents. We wish to avoid the danger that redistributors of a free
|
||||
program will individually obtain patent licenses, in effect making the
|
||||
program proprietary. To prevent this, we have made it clear that any
|
||||
patent must be licensed for everyone's free use or not licensed at all.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||
|
||||
0. This License applies to any program or other work which contains
|
||||
a notice placed by the copyright holder saying it may be distributed
|
||||
under the terms of this General Public License. The "Program", below,
|
||||
refers to any such program or work, and a "work based on the Program"
|
||||
means either the Program or any derivative work under copyright law:
|
||||
that is to say, a work containing the Program or a portion of it,
|
||||
either verbatim or with modifications and/or translated into another
|
||||
language. (Hereinafter, translation is included without limitation in
|
||||
the term "modification".) Each licensee is addressed as "you".
|
||||
|
||||
Activities other than copying, distribution and modification are not
|
||||
covered by this License; they are outside its scope. The act of
|
||||
running the Program is not restricted, and the output from the Program
|
||||
is covered only if its contents constitute a work based on the
|
||||
Program (independent of having been made by running the Program).
|
||||
Whether that is true depends on what the Program does.
|
||||
|
||||
1. You may copy and distribute verbatim copies of the Program's
|
||||
source code as you receive it, in any medium, provided that you
|
||||
conspicuously and appropriately publish on each copy an appropriate
|
||||
copyright notice and disclaimer of warranty; keep intact all the
|
||||
notices that refer to this License and to the absence of any warranty;
|
||||
and give any other recipients of the Program a copy of this License
|
||||
along with the Program.
|
||||
|
||||
You may charge a fee for the physical act of transferring a copy, and
|
||||
you may at your option offer warranty protection in exchange for a fee.
|
||||
|
||||
2. You may modify your copy or copies of the Program or any portion
|
||||
of it, thus forming a work based on the Program, and copy and
|
||||
distribute such modifications or work under the terms of Section 1
|
||||
above, provided that you also meet all of these conditions:
|
||||
|
||||
a) You must cause the modified files to carry prominent notices
|
||||
stating that you changed the files and the date of any change.
|
||||
|
||||
b) You must cause any work that you distribute or publish, that in
|
||||
whole or in part contains or is derived from the Program or any
|
||||
part thereof, to be licensed as a whole at no charge to all third
|
||||
parties under the terms of this License.
|
||||
|
||||
c) If the modified program normally reads commands interactively
|
||||
when run, you must cause it, when started running for such
|
||||
interactive use in the most ordinary way, to print or display an
|
||||
announcement including an appropriate copyright notice and a
|
||||
notice that there is no warranty (or else, saying that you provide
|
||||
a warranty) and that users may redistribute the program under
|
||||
these conditions, and telling the user how to view a copy of this
|
||||
License. (Exception: if the Program itself is interactive but
|
||||
does not normally print such an announcement, your work based on
|
||||
the Program is not required to print an announcement.)
|
||||
|
||||
These requirements apply to the modified work as a whole. If
|
||||
identifiable sections of that work are not derived from the Program,
|
||||
and can be reasonably considered independent and separate works in
|
||||
themselves, then this License, and its terms, do not apply to those
|
||||
sections when you distribute them as separate works. But when you
|
||||
distribute the same sections as part of a whole which is a work based
|
||||
on the Program, the distribution of the whole must be on the terms of
|
||||
this License, whose permissions for other licensees extend to the
|
||||
entire whole, and thus to each and every part regardless of who wrote it.
|
||||
|
||||
Thus, it is not the intent of this section to claim rights or contest
|
||||
your rights to work written entirely by you; rather, the intent is to
|
||||
exercise the right to control the distribution of derivative or
|
||||
collective works based on the Program.
|
||||
|
||||
In addition, mere aggregation of another work not based on the Program
|
||||
with the Program (or with a work based on the Program) on a volume of
|
||||
a storage or distribution medium does not bring the other work under
|
||||
the scope of this License.
|
||||
|
||||
3. You may copy and distribute the Program (or a work based on it,
|
||||
under Section 2) in object code or executable form under the terms of
|
||||
Sections 1 and 2 above provided that you also do one of the following:
|
||||
|
||||
a) Accompany it with the complete corresponding machine-readable
|
||||
source code, which must be distributed under the terms of Sections
|
||||
1 and 2 above on a medium customarily used for software interchange; or,
|
||||
|
||||
b) Accompany it with a written offer, valid for at least three
|
||||
years, to give any third party, for a charge no more than your
|
||||
cost of physically performing source distribution, a complete
|
||||
machine-readable copy of the corresponding source code, to be
|
||||
distributed under the terms of Sections 1 and 2 above on a medium
|
||||
customarily used for software interchange; or,
|
||||
|
||||
c) Accompany it with the information you received as to the offer
|
||||
to distribute corresponding source code. (This alternative is
|
||||
allowed only for noncommercial distribution and only if you
|
||||
received the program in object code or executable form with such
|
||||
an offer, in accord with Subsection b above.)
|
||||
|
||||
The source code for a work means the preferred form of the work for
|
||||
making modifications to it. For an executable work, complete source
|
||||
code means all the source code for all modules it contains, plus any
|
||||
associated interface definition files, plus the scripts used to
|
||||
control compilation and installation of the executable. However, as a
|
||||
special exception, the source code distributed need not include
|
||||
anything that is normally distributed (in either source or binary
|
||||
form) with the major components (compiler, kernel, and so on) of the
|
||||
operating system on which the executable runs, unless that component
|
||||
itself accompanies the executable.
|
||||
|
||||
If distribution of executable or object code is made by offering
|
||||
access to copy from a designated place, then offering equivalent
|
||||
access to copy the source code from the same place counts as
|
||||
distribution of the source code, even though third parties are not
|
||||
compelled to copy the source along with the object code.
|
||||
|
||||
4. You may not copy, modify, sublicense, or distribute the Program
|
||||
except as expressly provided under this License. Any attempt
|
||||
otherwise to copy, modify, sublicense or distribute the Program is
|
||||
void, and will automatically terminate your rights under this License.
|
||||
However, parties who have received copies, or rights, from you under
|
||||
this License will not have their licenses terminated so long as such
|
||||
parties remain in full compliance.
|
||||
|
||||
5. You are not required to accept this License, since you have not
|
||||
signed it. However, nothing else grants you permission to modify or
|
||||
distribute the Program or its derivative works. These actions are
|
||||
prohibited by law if you do not accept this License. Therefore, by
|
||||
modifying or distributing the Program (or any work based on the
|
||||
Program), you indicate your acceptance of this License to do so, and
|
||||
all its terms and conditions for copying, distributing or modifying
|
||||
the Program or works based on it.
|
||||
|
||||
6. Each time you redistribute the Program (or any work based on the
|
||||
Program), the recipient automatically receives a license from the
|
||||
original licensor to copy, distribute or modify the Program subject to
|
||||
these terms and conditions. You may not impose any further
|
||||
restrictions on the recipients' exercise of the rights granted herein.
|
||||
You are not responsible for enforcing compliance by third parties to
|
||||
this License.
|
||||
|
||||
7. If, as a consequence of a court judgment or allegation of patent
|
||||
infringement or for any other reason (not limited to patent issues),
|
||||
conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot
|
||||
distribute so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you
|
||||
may not distribute the Program at all. For example, if a patent
|
||||
license would not permit royalty-free redistribution of the Program by
|
||||
all those who receive copies directly or indirectly through you, then
|
||||
the only way you could satisfy both it and this License would be to
|
||||
refrain entirely from distribution of the Program.
|
||||
|
||||
If any portion of this section is held invalid or unenforceable under
|
||||
any particular circumstance, the balance of the section is intended to
|
||||
apply and the section as a whole is intended to apply in other
|
||||
circumstances.
|
||||
|
||||
It is not the purpose of this section to induce you to infringe any
|
||||
patents or other property right claims or to contest validity of any
|
||||
such claims; this section has the sole purpose of protecting the
|
||||
integrity of the free software distribution system, which is
|
||||
implemented by public license practices. Many people have made
|
||||
generous contributions to the wide range of software distributed
|
||||
through that system in reliance on consistent application of that
|
||||
system; it is up to the author/donor to decide if he or she is willing
|
||||
to distribute software through any other system and a licensee cannot
|
||||
impose that choice.
|
||||
|
||||
This section is intended to make thoroughly clear what is believed to
|
||||
be a consequence of the rest of this License.
|
||||
|
||||
8. If the distribution and/or use of the Program is restricted in
|
||||
certain countries either by patents or by copyrighted interfaces, the
|
||||
original copyright holder who places the Program under this License
|
||||
may add an explicit geographical distribution limitation excluding
|
||||
those countries, so that distribution is permitted only in or among
|
||||
countries not thus excluded. In such case, this License incorporates
|
||||
the limitation as if written in the body of this License.
|
||||
|
||||
9. The Free Software Foundation may publish revised and/or new versions
|
||||
of the General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the Program
|
||||
specifies a version number of this License which applies to it and "any
|
||||
later version", you have the option of following the terms and conditions
|
||||
either of that version or of any later version published by the Free
|
||||
Software Foundation. If the Program does not specify a version number of
|
||||
this License, you may choose any version ever published by the Free Software
|
||||
Foundation.
|
||||
|
||||
10. If you wish to incorporate parts of the Program into other free
|
||||
programs whose distribution conditions are different, write to the author
|
||||
to ask for permission. For software which is copyrighted by the Free
|
||||
Software Foundation, write to the Free Software Foundation; we sometimes
|
||||
make exceptions for this. Our decision will be guided by the two goals
|
||||
of preserving the free status of all derivatives of our free software and
|
||||
of promoting the sharing and reuse of software generally.
|
||||
|
||||
NO WARRANTY
|
||||
|
||||
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
|
||||
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
|
||||
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
|
||||
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
|
||||
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
|
||||
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
|
||||
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
|
||||
REPAIR OR CORRECTION.
|
||||
|
||||
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
|
||||
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
|
||||
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
|
||||
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
|
||||
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
|
||||
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
|
||||
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
|
||||
POSSIBILITY OF SUCH DAMAGES.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
convey the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License along
|
||||
with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program is interactive, make it output a short notice like this
|
||||
when it starts in an interactive mode:
|
||||
|
||||
Gnomovision version 69, Copyright (C) year name of author
|
||||
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, the commands you use may
|
||||
be called something other than `show w' and `show c'; they could even be
|
||||
mouse-clicks or menu items--whatever suits your program.
|
||||
|
||||
You should also get your employer (if you work as a programmer) or your
|
||||
school, if any, to sign a "copyright disclaimer" for the program, if
|
||||
necessary. Here is a sample; alter the names:
|
||||
|
||||
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
|
||||
`Gnomovision' (which makes passes at compilers) written by James Hacker.
|
||||
|
||||
<signature of Ty Coon>, 1 April 1989
|
||||
Ty Coon, President of Vice
|
||||
|
||||
This General Public License does not permit incorporating your program into
|
||||
proprietary programs. If your program is a subroutine library, you may
|
||||
consider it more useful to permit linking proprietary applications with the
|
||||
library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License.
|
44
Makefile
Normal file
44
Makefile
Normal file
@ -0,0 +1,44 @@
|
||||
CC = gcc
|
||||
CFLAGS = -O2 -s -D_GNU_SOURCE
|
||||
LDLIBS = -lcyassl -lm
|
||||
PREFIX =
|
||||
BINDIR = $(PREFIX)/usr/bin
|
||||
|
||||
ifeq ($(CROSS_BUILD), 1)
|
||||
DEFS:=-DCROSS_BUILD
|
||||
else
|
||||
DEFS:=
|
||||
endif
|
||||
|
||||
all: webget webpost
|
||||
@echo "All done!"
|
||||
|
||||
webget: webget.o libweb.o
|
||||
$(CC) $(CFLAGS) $(LDLIBS) webget.o libweb.o -o webget
|
||||
|
||||
webpost: webpost.o libweb.o
|
||||
$(CC) $(CFLAGS) $(LDLIBS) webpost.o libweb.o -o webpost
|
||||
|
||||
webget.o: webget.c
|
||||
$(CC) $(CFLAGS) $(DEFS) -c webget.c
|
||||
|
||||
webpost.o: webpost.c
|
||||
$(CC) $(CFLAGS) $(DEFS) -c webpost.c
|
||||
|
||||
libweb.o: libweb.c
|
||||
$(CC) $(CFLAGS) $(DEFS) -c libweb.c
|
||||
|
||||
install:
|
||||
mkdir -p $(BINDIR)
|
||||
cp webget $(BINDIR)
|
||||
cp webpost $(BINDIR)
|
||||
@echo "All done!"
|
||||
|
||||
uninstall:
|
||||
@rm -f $(BINDIR)/webget
|
||||
@rm -f $(BINDIR)/webpost
|
||||
@echo "All done!"
|
||||
|
||||
clean:
|
||||
rm -f webget webpost *.o
|
||||
@echo "All done!"
|
21
README
Normal file
21
README
Normal file
@ -0,0 +1,21 @@
|
||||
WebGet
|
||||
======
|
||||
|
||||
WebGet fetches an HTTP and HTTPS URLs and dumps the contents to stdout. It does
|
||||
not do gopher, ftp, file, news, or any other type of URL, only HTTP. It can be
|
||||
configured to do HTTPS fetches as well.
|
||||
|
||||
|
||||
It is based on http_get <http://acme.com/software/http_get/>
|
||||
|
||||
|
||||
|
||||
WebPost
|
||||
=======
|
||||
|
||||
WebPost does a POST operation to an HTTP and HTTPS URLs and dumps the results to
|
||||
stdout. It does not do gopher, ftp, file, news, or any other type of URL, only
|
||||
HTTP. It can be configured to do HTTPS POSTs as well.
|
||||
|
||||
|
||||
It is based on http_post <http://acme.com/software/http_post/>
|
823
libweb.c
Normal file
823
libweb.c
Normal file
@ -0,0 +1,823 @@
|
||||
/**
|
||||
* @PROJECT WebGet Downloader
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE libweb.c
|
||||
* @PURPOSE Common Web Library
|
||||
* @DEVELOPERS Al Globus <globus@nas.nasa.gov>
|
||||
* Rafal Kupiec <belliash@asiotec.eu.org>
|
||||
* Jef Poskanzer <jef@mail.acme.com>
|
||||
*/
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <signal.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
|
||||
#include <cyassl/openssl/ssl.h>
|
||||
#include <cyassl/openssl/err.h>
|
||||
#include <cyassl/error.h>
|
||||
|
||||
#ifdef CROSS_BUILD
|
||||
#include <cyassl/cyassl_error.h>
|
||||
#endif
|
||||
|
||||
#include "libweb.h"
|
||||
|
||||
int b64_encode(unsigned char* ptr, int len, char* space, int size) {
|
||||
int ptr_idx, space_idx, phase;
|
||||
char c;
|
||||
|
||||
space_idx = 0;
|
||||
phase = 0;
|
||||
for(ptr_idx = 0; ptr_idx < len; ++ptr_idx) {
|
||||
switch(phase) {
|
||||
case 0:
|
||||
c = b64_encode_table[ptr[ptr_idx] >> 2];
|
||||
if(space_idx < size) {
|
||||
space[space_idx++] = c;
|
||||
}
|
||||
c = b64_encode_table[(ptr[ptr_idx] & 0x3) << 4];
|
||||
if(space_idx < size) {
|
||||
space[space_idx++] = c;
|
||||
}
|
||||
++phase;
|
||||
break;
|
||||
case 1:
|
||||
space[space_idx - 1] = b64_encode_table[b64_decode_table[(int) ((unsigned char) space[space_idx - 1])] | (ptr[ptr_idx] >> 4)];
|
||||
c = b64_encode_table[(ptr[ptr_idx] & 0xf) << 2];
|
||||
if(space_idx < size) {
|
||||
space[space_idx++] = c;
|
||||
}
|
||||
++phase;
|
||||
break;
|
||||
case 2:
|
||||
space[space_idx - 1] = b64_encode_table[b64_decode_table[(int) ((unsigned char) space[space_idx - 1])] | (ptr[ptr_idx] >> 6)];
|
||||
c = b64_encode_table[ptr[ptr_idx] & 0x3f];
|
||||
if(space_idx < size) {
|
||||
space[space_idx++] = c;
|
||||
}
|
||||
phase = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
while(phase++ < 3) {
|
||||
if(space_idx < size) {
|
||||
space[space_idx++] = '=';
|
||||
}
|
||||
}
|
||||
return space_idx;
|
||||
}
|
||||
|
||||
void check(void* ptr) {
|
||||
if(ptr == (void*) 0) {
|
||||
(void) fprintf(stderr, "%s: out of memory\n", argv0);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
off_t file_bytes(const char* filename) {
|
||||
struct stat sb;
|
||||
|
||||
if(stat(filename, &sb) < 0) {
|
||||
perror(filename);
|
||||
exit(1);
|
||||
}
|
||||
return sb.st_size;
|
||||
}
|
||||
|
||||
int file_copy(const char* filename, char* buf) {
|
||||
int fd;
|
||||
struct stat sb;
|
||||
off_t bytes;
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if(fd == -1) {
|
||||
perror(filename);
|
||||
exit(-1);
|
||||
}
|
||||
if(fstat(fd, &sb) != 0) {
|
||||
perror(filename);
|
||||
exit(-1);
|
||||
}
|
||||
bytes = sb.st_size;
|
||||
if(read(fd, buf, bytes) != bytes) {
|
||||
perror(filename);
|
||||
exit(-1);
|
||||
}
|
||||
(void) close(fd);
|
||||
return bytes;
|
||||
}
|
||||
|
||||
int getURL(char* url, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value) {
|
||||
char* s;
|
||||
int protocol;
|
||||
char host[2000];
|
||||
int host_len;
|
||||
unsigned short port;
|
||||
char* file = (char*) 0;
|
||||
char* http = "http://";
|
||||
int http_len = strlen( http );
|
||||
char* https = "https://";
|
||||
int https_len = strlen( https );
|
||||
int proto_len;
|
||||
|
||||
if(url == (char*) 0) {
|
||||
(void) fprintf(stderr, "%s: null URL\n", argv0);
|
||||
exit(1);
|
||||
}
|
||||
if(strncmp(http, url, http_len) == 0) {
|
||||
proto_len = http_len;
|
||||
protocol = PROTO_HTTP;
|
||||
} else if(strncmp(https, url, https_len) == 0) {
|
||||
proto_len = https_len;
|
||||
protocol = PROTO_HTTPS;
|
||||
} else {
|
||||
(void) fprintf(stderr, "%s: non-http URL\n", argv0);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for(s = url + proto_len; *s != '\0' && *s != ':' && *s != '/'; ++s);
|
||||
host_len = s - url;
|
||||
host_len -= proto_len;
|
||||
strncpy(host, url + proto_len, host_len);
|
||||
host[host_len] = '\0';
|
||||
if(*s == ':') {
|
||||
port = (unsigned short) atoi(++s);
|
||||
while(*s != '\0' && *s != '/')
|
||||
++s;
|
||||
} else {
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
port = 443;
|
||||
} else {
|
||||
port = 80;
|
||||
}
|
||||
}
|
||||
if(*s == '\0') {
|
||||
file = "/";
|
||||
} else {
|
||||
file = s;
|
||||
}
|
||||
return getURLbyParts(protocol, host, port, file, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value);
|
||||
}
|
||||
|
||||
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) {
|
||||
int sockfd;
|
||||
SSL_CTX* ssl_ctx = (SSL_CTX*) 0;
|
||||
SSL* ssl = (SSL*) 0;
|
||||
char buf[20000];
|
||||
int i, bytes, b, header_state, status;
|
||||
|
||||
(void) alarm(timeout);
|
||||
sockfd = open_client_socket(host, port);
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
int r;
|
||||
ssl_ctx = SSL_CTX_new(TLSv1_client_method());
|
||||
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, 0);
|
||||
ssl = SSL_new(ssl_ctx);
|
||||
SSL_set_fd(ssl, sockfd);
|
||||
r = SSL_connect(ssl);
|
||||
if(r <= 0) {
|
||||
(void) fprintf(stderr, "%s: %s - SSL connection failed - %d\n", argv0, url, r);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
(void) alarm(timeout);
|
||||
bytes = snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n", file);
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Host: %s\r\n", host);
|
||||
if(referer != (char*) 0) {
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Referer: %s\r\n", referer);
|
||||
}
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "User-Agent: %s\r\n", user_agent);
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept: */*\r\n");
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept-Language: en\r\n");
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Accept-Charset: iso-8859-1,*,utf-8\r\n");
|
||||
if(auth_token != (char*) 0) {
|
||||
char token_buf[1000];
|
||||
token_buf[b64_encode((unsigned char*) auth_token, strlen( auth_token ), token_buf, sizeof(token_buf))] = '\0';
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Authorization: Basic %s\r\n", token_buf);
|
||||
}
|
||||
for(i = 0; i < ncookies; ++i) {
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "Cookie: %s\r\n", cookies[i]);
|
||||
}
|
||||
if(header_name != (char*) 0) {
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "%s: %s\r\n", header_name, header_value);
|
||||
}
|
||||
bytes += snprintf(&buf[bytes], sizeof(buf) - bytes, "\r\n");
|
||||
if (protocol == PROTO_HTTPS)
|
||||
(void) SSL_write(ssl, buf, bytes);
|
||||
else
|
||||
(void) write(sockfd, buf, bytes);
|
||||
(void) alarm(timeout);
|
||||
header_state = HDST_LINE1_PROTOCOL;
|
||||
status = 0;
|
||||
for(;;) {
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
bytes = SSL_read(ssl, buf, sizeof(buf));
|
||||
} else {
|
||||
bytes = read(sockfd, buf, sizeof(buf));
|
||||
}
|
||||
if(bytes <= 0) {
|
||||
break;
|
||||
}
|
||||
for(b = 0; b < bytes; ++b) {
|
||||
if(verbose) {
|
||||
(void) write(1, &buf[b], 1);
|
||||
}
|
||||
switch(header_state) {
|
||||
case HDST_LINE1_PROTOCOL:
|
||||
switch(buf[b]) {
|
||||
case ' ': case '\t':
|
||||
header_state = HDST_LINE1_WHITESPACE;
|
||||
break;
|
||||
case '\n':
|
||||
header_state = HDST_LF;
|
||||
break;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_LINE1_WHITESPACE:
|
||||
switch(buf[b]) {
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
status = buf[b] - '0';
|
||||
header_state = HDST_LINE1_STATUS;
|
||||
break;
|
||||
case '\n':
|
||||
header_state = HDST_LF;
|
||||
break;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_LINE1_STATUS:
|
||||
switch(buf[b]) {
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
status = status * 10 + buf[b] - '0';
|
||||
break;
|
||||
case '\n':
|
||||
header_state = HDST_LF;
|
||||
break;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_BOL:
|
||||
switch(buf[b]) {
|
||||
case '\n':
|
||||
header_state = HDST_LF;
|
||||
break;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_TEXT:
|
||||
switch(buf[b]) {
|
||||
case '\n':
|
||||
header_state = HDST_LF;
|
||||
break;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_LF:
|
||||
switch(buf[b]) {
|
||||
case '\n':
|
||||
goto end_of_headers;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_CR:
|
||||
switch(buf[b]) {
|
||||
case '\n':
|
||||
header_state = HDST_CRLF;
|
||||
break;
|
||||
case '\r':
|
||||
goto end_of_headers;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_CRLF:
|
||||
switch(buf[b]) {
|
||||
case '\n':
|
||||
goto end_of_headers;
|
||||
case '\r':
|
||||
header_state = HDST_CRLFCR;
|
||||
break;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_CRLFCR:
|
||||
switch(buf[b]) {
|
||||
case '\n':
|
||||
case '\r':
|
||||
goto end_of_headers;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
end_of_headers:
|
||||
if(bytes > 0) {
|
||||
++b;
|
||||
(void) write(1, &buf[b], bytes - b);
|
||||
}
|
||||
for(;;) {
|
||||
(void) alarm(timeout);
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
bytes = SSL_read(ssl, buf, sizeof(buf));
|
||||
} else {
|
||||
bytes = read(sockfd, buf, sizeof(buf));
|
||||
}
|
||||
if(bytes == 0) {
|
||||
break;
|
||||
}
|
||||
if(bytes < 0) {
|
||||
show_error("read");
|
||||
}
|
||||
(void) write(1, buf, bytes);
|
||||
}
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
SSL_free(ssl);
|
||||
SSL_CTX_free(ssl_ctx);
|
||||
}
|
||||
(void) close(sockfd);
|
||||
return status;
|
||||
}
|
||||
|
||||
void* malloc_check(size_t size) {
|
||||
void* ptr = malloc(size);
|
||||
check(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
int open_client_socket(char* hostname, unsigned short port) {
|
||||
struct addrinfo hints;
|
||||
char portstr[10];
|
||||
int gaierr;
|
||||
struct addrinfo* ai;
|
||||
struct addrinfo* ai2;
|
||||
struct addrinfo* aiv4;
|
||||
struct addrinfo* aiv6;
|
||||
struct sockaddr_in6 sa;
|
||||
int sa_len, sock_family, sock_type, sock_protocol;
|
||||
int sockfd;
|
||||
|
||||
(void) memset((void*) &sa, 0, sizeof(sa));
|
||||
(void) memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = PF_UNSPEC;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
(void) snprintf(portstr, sizeof(portstr), "%d", (int) port);
|
||||
if((gaierr = getaddrinfo(hostname, portstr, &hints, &ai)) != 0) {
|
||||
(void) fprintf(stderr, "%s: getaddrinfo %s - %s\n", argv0, hostname, gai_strerror(gaierr));
|
||||
exit(1);
|
||||
}
|
||||
aiv4 = (struct addrinfo*) 0;
|
||||
aiv6 = (struct addrinfo*) 0;
|
||||
for(ai2 = ai; ai2 != (struct addrinfo*) 0; ai2 = ai2->ai_next) {
|
||||
switch(ai2->ai_family) {
|
||||
case AF_INET:
|
||||
if(aiv4 == (struct addrinfo*) 0) {
|
||||
aiv4 = ai2;
|
||||
}
|
||||
break;
|
||||
case AF_INET6:
|
||||
if(aiv6 == (struct addrinfo*) 0) {
|
||||
aiv6 = ai2;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(aiv4 != (struct addrinfo*) 0) {
|
||||
if(sizeof(sa) < aiv4->ai_addrlen) {
|
||||
(void) fprintf(stderr, "%s - sockaddr too small (%lu < %lu)\n", hostname, (unsigned long) sizeof(sa), (unsigned long) aiv4->ai_addrlen);
|
||||
exit(1);
|
||||
}
|
||||
sock_family = aiv4->ai_family;
|
||||
sock_type = aiv4->ai_socktype;
|
||||
sock_protocol = aiv4->ai_protocol;
|
||||
sa_len = aiv4->ai_addrlen;
|
||||
(void) memmove(&sa, aiv4->ai_addr, sa_len);
|
||||
goto ok;
|
||||
}
|
||||
if(aiv6 != (struct addrinfo*) 0) {
|
||||
if(sizeof(sa) < aiv6->ai_addrlen) {
|
||||
(void) fprintf(stderr, "%s - sockaddr too small (%lu < %lu)\n", hostname, (unsigned long) sizeof(sa), (unsigned long) aiv6->ai_addrlen);
|
||||
exit(1);
|
||||
}
|
||||
sock_family = aiv6->ai_family;
|
||||
sock_type = aiv6->ai_socktype;
|
||||
sock_protocol = aiv6->ai_protocol;
|
||||
sa_len = aiv6->ai_addrlen;
|
||||
(void) memmove(&sa, aiv6->ai_addr, sa_len);
|
||||
goto ok;
|
||||
}
|
||||
(void) fprintf(stderr, "%s: no valid address found for host %s\n", argv0, hostname);
|
||||
exit(1);
|
||||
ok:
|
||||
freeaddrinfo(ai);
|
||||
sockfd = socket(sock_family, sock_type, sock_protocol);
|
||||
if(sockfd < 0) {
|
||||
show_error("socket");
|
||||
}
|
||||
if(connect(sockfd, (struct sockaddr*) &sa, sa_len) < 0) {
|
||||
show_error("connect");
|
||||
}
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
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) {
|
||||
char* s;
|
||||
int protocol;
|
||||
char host[2000];
|
||||
int host_len;
|
||||
unsigned short port;
|
||||
char* file = 0;
|
||||
char* http = "http://";
|
||||
int http_len = strlen(http);
|
||||
char* https = "https://";
|
||||
int https_len = strlen(https);
|
||||
int proto_len;
|
||||
|
||||
if(url == (char*) 0) {
|
||||
(void) fprintf(stderr, "%s: null URL\n", argv0);
|
||||
exit(1);
|
||||
}
|
||||
if(strncmp(http, url, http_len) == 0) {
|
||||
proto_len = http_len;
|
||||
protocol = PROTO_HTTP;
|
||||
} else if(strncmp(https, url, https_len) == 0) {
|
||||
proto_len = https_len;
|
||||
protocol = PROTO_HTTPS;
|
||||
} else {
|
||||
(void) fprintf(stderr, "%s: non-http URL\n", argv0);
|
||||
exit(1);
|
||||
}
|
||||
for(s = url + proto_len; *s != '\0' && *s != ':' && *s != '/'; ++s);
|
||||
host_len = s - url;
|
||||
host_len -= proto_len;
|
||||
strncpy(host, url + proto_len, host_len);
|
||||
host[host_len] = '\0';
|
||||
if(*s == ':') {
|
||||
port = (unsigned short) atoi(++s);
|
||||
while(*s != '\0' && *s != '/') {
|
||||
++s;
|
||||
}
|
||||
} else {
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
port = 443;
|
||||
} else {
|
||||
port = 80;
|
||||
}
|
||||
}
|
||||
if(*s == '\0') {
|
||||
file = "/";
|
||||
} else {
|
||||
file = s;
|
||||
}
|
||||
postURLbyParts(protocol, host, port, file, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value, args, argc);
|
||||
}
|
||||
|
||||
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) {
|
||||
int sockfd;
|
||||
SSL_CTX* ssl_ctx = (SSL_CTX*) 0;
|
||||
SSL* ssl = (SSL*) 0;
|
||||
char head_buf[20000];
|
||||
int max_arg, total_bytes;
|
||||
int multipart, next_arg_is_file;
|
||||
static const char* const sep = "http_post-content-separator";
|
||||
char* data_buf;
|
||||
char* enc_buf;
|
||||
int head_bytes, data_bytes, i, header_state;
|
||||
char* eq;
|
||||
|
||||
(void) alarm(timeout);
|
||||
sockfd = open_client_socket(host, port);
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
int r;
|
||||
ssl_ctx = SSL_CTX_new(TLSv1_client_method());
|
||||
SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_NONE, 0);
|
||||
ssl = SSL_new(ssl_ctx);
|
||||
SSL_set_fd(ssl, sockfd);
|
||||
r = SSL_connect(ssl);
|
||||
if(r <= 0) {
|
||||
(void) fprintf(stderr, "%s: %s - SSL connection failed - %d\n", argv0, url, r);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
multipart = 0;
|
||||
total_bytes = max_arg = 0;
|
||||
next_arg_is_file = 0;
|
||||
for(i = 0; i < argc ; ++i) {
|
||||
int l = strlen(args[i]);
|
||||
if(strcmp(args[i], "-f") == 0) {
|
||||
multipart = 1;
|
||||
next_arg_is_file = 1;
|
||||
continue;
|
||||
}
|
||||
total_bytes += l;
|
||||
if(l > max_arg) {
|
||||
max_arg = l;
|
||||
}
|
||||
if(next_arg_is_file) {
|
||||
eq = strchr(args[i], '=');
|
||||
if(eq == (char*) 0) {
|
||||
(void) fprintf(stderr, "%s: missing filename\n", argv0);
|
||||
exit(1);
|
||||
} else {
|
||||
++eq;
|
||||
total_bytes += file_bytes(eq);
|
||||
}
|
||||
next_arg_is_file = 0;
|
||||
}
|
||||
}
|
||||
if(multipart) {
|
||||
for(i = 0; i < argc ; ++i) {
|
||||
total_bytes += strlen(sep) * 2 + 100;
|
||||
}
|
||||
total_bytes += strlen(sep) * 2;
|
||||
} else {
|
||||
total_bytes *= 4;
|
||||
enc_buf = (char*) malloc_check(max_arg * 4);
|
||||
}
|
||||
data_buf = (char*) malloc_check(total_bytes);
|
||||
if(multipart) {
|
||||
next_arg_is_file = 0;
|
||||
data_bytes = 0;
|
||||
for(i = 0; i < argc; ++i) {
|
||||
if(strcmp(args[i], "-f") == 0) {
|
||||
next_arg_is_file = 1;
|
||||
continue;
|
||||
}
|
||||
eq = strchr(args[i], '=');
|
||||
if(eq == (char*) 0) {
|
||||
data_bytes += sprintf(&data_buf[data_bytes], "--%s\r\nContent-Disposition: form-data\r\n\r\n%s\r\n", sep, args[i]);
|
||||
} else {
|
||||
*eq++ = '\0';
|
||||
if(next_arg_is_file) {
|
||||
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);
|
||||
data_bytes += file_copy(eq, &data_buf[data_bytes]);
|
||||
data_bytes += sprintf(&data_buf[data_bytes], "\r\n");
|
||||
next_arg_is_file = 0;
|
||||
} else {
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
data_bytes += sprintf(&data_buf[data_bytes], "--%s--\r\n", sep);
|
||||
} else {
|
||||
data_bytes = 0;
|
||||
for(i = 0; i < argc ; ++i) {
|
||||
if(data_bytes > 0) {
|
||||
data_bytes += sprintf(&data_buf[data_bytes], "&");
|
||||
}
|
||||
eq = strchr(args[i], '=');
|
||||
if(eq == (char*) 0) {
|
||||
strencode(enc_buf, args[i]);
|
||||
data_bytes += sprintf(&data_buf[data_bytes], "%s", enc_buf);
|
||||
} else {
|
||||
*eq++ = '\0';
|
||||
strencode(enc_buf, args[i]);
|
||||
data_bytes += sprintf(&data_buf[data_bytes], "%s=", enc_buf);
|
||||
strencode(enc_buf, eq);
|
||||
data_bytes += sprintf(&data_buf[data_bytes], "%s", enc_buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
(void) alarm(timeout);
|
||||
head_bytes = snprintf(head_buf, sizeof(head_buf), "POST %s HTTP/1.0\r\n", file);
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Host: %s\r\n", host);
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-Length: %d\r\n", data_bytes);
|
||||
if(referer != (char*) 0) {
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Referer: %s\r\n", referer);
|
||||
}
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "User-Agent: %s\r\n", user_agent);
|
||||
if(multipart) {
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-type: multipart/form-data; boundary=\"%s\"\r\n", sep);
|
||||
} else {
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Content-type: application/x-www-form-urlencoded\r\n");
|
||||
}
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept: */*\r\n");
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept-Language: en\r\n");
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Accept-Charset: iso-8859-1,*,utf-8\r\n");
|
||||
if(auth_token != (char*) 0) {
|
||||
char token_buf[1000];
|
||||
token_buf[b64_encode((unsigned char*) auth_token, strlen(auth_token), token_buf, sizeof(token_buf))] = '\0';
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Authorization: Basic %s\r\n", token_buf);
|
||||
}
|
||||
for(i = 0; i < ncookies; ++i) {
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "Cookie: %s\r\n", cookies[i]);
|
||||
}
|
||||
if(header_name != (char*) 0) {
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "%s: %s\r\n", header_name, header_value);
|
||||
}
|
||||
head_bytes += snprintf(&head_buf[head_bytes], sizeof(head_buf) - head_bytes, "\r\n");
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
(void) SSL_write(ssl, head_buf, head_bytes);
|
||||
} else {
|
||||
(void) write(sockfd, head_buf, head_bytes);
|
||||
}
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
(void) SSL_write(ssl, data_buf, data_bytes);
|
||||
} else {
|
||||
(void) write(sockfd, data_buf, data_bytes);
|
||||
}
|
||||
(void) alarm(timeout);
|
||||
header_state = HDST_BOL;
|
||||
for(;;) {
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
head_bytes = SSL_read(ssl, head_buf, sizeof(head_buf));
|
||||
} else {
|
||||
head_bytes = read(sockfd, head_buf, sizeof(head_buf));
|
||||
}
|
||||
if(head_bytes <= 0) {
|
||||
break;
|
||||
}
|
||||
for(i = 0; i < head_bytes; ++i) {
|
||||
if(verbose) {
|
||||
(void) write(1, &head_buf[i], 1);
|
||||
}
|
||||
switch(header_state) {
|
||||
case HDST_BOL:
|
||||
switch(head_buf[i]) {
|
||||
case '\n':
|
||||
header_state = HDST_LF;
|
||||
break;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_TEXT:
|
||||
switch(head_buf[i]) {
|
||||
case '\n':
|
||||
header_state = HDST_LF;
|
||||
break;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_LF:
|
||||
switch(head_buf[i]) {
|
||||
case '\n':
|
||||
goto end_of_headers;
|
||||
case '\r':
|
||||
header_state = HDST_CR;
|
||||
break;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_CR:
|
||||
switch(head_buf[i]) {
|
||||
case '\n':
|
||||
header_state = HDST_CRLF;
|
||||
break;
|
||||
case '\r':
|
||||
goto end_of_headers;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_CRLF:
|
||||
switch(head_buf[i]) {
|
||||
case '\n':
|
||||
goto end_of_headers;
|
||||
case '\r':
|
||||
header_state = HDST_CRLFCR;
|
||||
break;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
case HDST_CRLFCR:
|
||||
switch(head_buf[i]) {
|
||||
case '\n':
|
||||
case '\r':
|
||||
goto end_of_headers;
|
||||
default:
|
||||
header_state = HDST_TEXT;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
end_of_headers:
|
||||
if(head_bytes > 0) {
|
||||
++i;
|
||||
(void) write(1, &head_buf[i], head_bytes - i);
|
||||
}
|
||||
for(;;) {
|
||||
(void) alarm(timeout);
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
head_bytes = SSL_read(ssl, head_buf, sizeof(head_buf));
|
||||
} else {
|
||||
head_bytes = read(sockfd, head_buf, sizeof(head_buf));
|
||||
}
|
||||
if(head_bytes == 0) {
|
||||
break;
|
||||
}
|
||||
if(head_bytes < 0) {
|
||||
show_error("read");
|
||||
}
|
||||
(void) write(1, head_buf, head_bytes);
|
||||
}
|
||||
if(protocol == PROTO_HTTPS) {
|
||||
SSL_free(ssl);
|
||||
SSL_CTX_free(ssl_ctx);
|
||||
}
|
||||
(void) close(sockfd);
|
||||
}
|
||||
|
||||
void show_error(char* cause) {
|
||||
char buf[5000];
|
||||
(void) sprintf(buf, "%s: %s - %s", argv0, url, cause);
|
||||
perror(buf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void sigcatch(int sig) {
|
||||
(void) fprintf(stderr, "%s: %s - timed out\n", argv0, url);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void strencode(char* to, char* from) {
|
||||
int tolen;
|
||||
|
||||
for(tolen = 0; *from != '\0'; ++from) {
|
||||
if(isalnum(*from) || strchr("/_.", *from) != (char*) 0) {
|
||||
*to = *from;
|
||||
++to;
|
||||
++tolen;
|
||||
} else {
|
||||
(void) sprintf(to, "%%%02x", (int) *from & 0xff);
|
||||
to += 3;
|
||||
tolen += 3;
|
||||
}
|
||||
}
|
||||
*to = '\0';
|
||||
}
|
||||
|
||||
void usage() {
|
||||
(void) fprintf(stderr, "usage: %s [-c cookie] [-t timeout] [-r referer] [-u user-agent] [-a username:password] [-h header value] [-v] url\n", argv0);
|
||||
exit(1);
|
||||
}
|
79
libweb.h
Normal file
79
libweb.h
Normal file
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* @PROJECT WebGet Downloader
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE libweb.h
|
||||
* @PURPOSE Common Web Library
|
||||
* @DEVELOPERS Al Globus <globus@nas.nasa.gov>
|
||||
* Rafal Kupiec <belliash@asiotec.eu.org>
|
||||
* Jef Poskanzer <jef@mail.acme.com>
|
||||
*/
|
||||
|
||||
#ifndef __LIBWEB_H
|
||||
#define __LIBWEB_H
|
||||
|
||||
#define HDST_LINE1_PROTOCOL 0
|
||||
#define HDST_LINE1_WHITESPACE 1
|
||||
#define HDST_LINE1_STATUS 2
|
||||
#define HDST_BOL 10
|
||||
#define HDST_TEXT 11
|
||||
#define HDST_LF 12
|
||||
#define HDST_CR 13
|
||||
#define HDST_CRLF 14
|
||||
#define HDST_CRLFCR 15
|
||||
|
||||
#define PROTO_HTTP 0
|
||||
#define PROTO_HTTPS 1
|
||||
|
||||
#define MAX_COOKIES 20
|
||||
|
||||
char* argv0;
|
||||
int timeout;
|
||||
char* url;
|
||||
int verbose;
|
||||
|
||||
static int b64_decode_table[256] = {
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 00-0F */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 10-1F */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, /* 20-2F */
|
||||
52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, /* 30-3F */
|
||||
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, /* 40-4F */
|
||||
15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, /* 50-5F */
|
||||
-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, /* 60-6F */
|
||||
41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, /* 70-7F */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 80-8F */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* 90-9F */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* A0-AF */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* B0-BF */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* C0-CF */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* D0-DF */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, /* E0-EF */
|
||||
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 /* F0-FF */
|
||||
};
|
||||
|
||||
static char b64_encode_table[64] = {
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', /* 0-7 */
|
||||
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', /* 8-15 */
|
||||
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', /* 16-23 */
|
||||
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', /* 24-31 */
|
||||
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', /* 32-39 */
|
||||
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', /* 40-47 */
|
||||
'w', 'x', 'y', 'z', '0', '1', '2', '3', /* 48-55 */
|
||||
'4', '5', '6', '7', '8', '9', '+', '/' /* 56-63 */
|
||||
};
|
||||
|
||||
int b64_encode(unsigned char* ptr, int len, char* space, int size);
|
||||
void check(void* ptr);
|
||||
off_t file_bytes(const char* filename);
|
||||
int file_copy(const char* filename, char* buf);
|
||||
int getURL(char* url, char* referer, char* user_agent, char* auth_token, int ncookies, char** cookies, char* header_name, char* header_value);
|
||||
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);
|
||||
void* malloc_check(size_t size);
|
||||
int open_client_socket(char* hostname, unsigned short port);
|
||||
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);
|
||||
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);
|
||||
void show_error(char* cause);
|
||||
void sigcatch(int sig);
|
||||
void strencode(char* to, char* from);
|
||||
void usage();
|
||||
|
||||
#endif
|
89
webget.c
Normal file
89
webget.c
Normal file
@ -0,0 +1,89 @@
|
||||
/**
|
||||
* @PROJECT WebGet Downloader
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE webget.c
|
||||
* @PURPOSE Web To StdOut Downloader
|
||||
* @DEVELOPERS Al Globus <globus@nas.nasa.gov>
|
||||
* Rafal Kupiec <belliash@asiotec.eu.org>
|
||||
* Jef Poskanzer <jef@mail.acme.com>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "libweb.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int argn;
|
||||
char* referer;
|
||||
char* user_agent;
|
||||
char* auth_token;
|
||||
int ncookies;
|
||||
char* cookies[MAX_COOKIES];
|
||||
char* header_name;
|
||||
char* header_value;
|
||||
int status;
|
||||
|
||||
argv0 = argv[0];
|
||||
argn = 1;
|
||||
timeout = 60;
|
||||
referer = (char*) 0;
|
||||
user_agent = "http_get";
|
||||
auth_token = (char*) 0;
|
||||
ncookies = 0;
|
||||
header_name = (char*) 0;
|
||||
header_value = (char*) 0;
|
||||
verbose = 0;
|
||||
|
||||
while(argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
|
||||
if(strcmp(argv[argn], "-t") == 0 && argn + 1 < argc) {
|
||||
++argn;
|
||||
timeout = atoi(argv[argn]);
|
||||
} else if(strcmp(argv[argn], "-r") == 0 && argn + 1 < argc) {
|
||||
++argn;
|
||||
referer = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-u") == 0 && argn + 1 < argc) {
|
||||
++argn;
|
||||
user_agent = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-a") == 0 && argn + 1 < argc) {
|
||||
++argn;
|
||||
auth_token = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-c") == 0 && argn + 1 < argc) {
|
||||
if(ncookies >= MAX_COOKIES) {
|
||||
(void) fprintf(stderr, "%s: too many cookies\n", argv0);
|
||||
exit (1);
|
||||
}
|
||||
++argn;
|
||||
cookies[ncookies++] = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-h") == 0 && argn + 2 < argc) {
|
||||
++argn;
|
||||
header_name = argv[argn];
|
||||
++argn;
|
||||
header_value = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-v") == 0) {
|
||||
verbose = 1;
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
++argn;
|
||||
}
|
||||
if(argn >= argc) {
|
||||
usage();
|
||||
}
|
||||
url = argv[argn];
|
||||
++argn;
|
||||
if(argn != argc) {
|
||||
usage();
|
||||
}
|
||||
|
||||
(void) signal(SIGALRM, sigcatch);
|
||||
status = getURL(url, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value);
|
||||
if(status == 200) {
|
||||
exit(0);
|
||||
} else if(status == 0) {
|
||||
exit(1);
|
||||
} else {
|
||||
exit(status);
|
||||
}
|
||||
}
|
79
webpost.c
Normal file
79
webpost.c
Normal file
@ -0,0 +1,79 @@
|
||||
/**
|
||||
* @PROJECT WebGet Downloader
|
||||
* @COPYRIGHT See COPYING in the top level directory
|
||||
* @FILE webpost.c
|
||||
* @PURPOSE Web POST Operation Dumper
|
||||
* @DEVELOPERS Al Globus <globus@nas.nasa.gov>
|
||||
* Rafal Kupiec <belliash@asiotec.eu.org>
|
||||
* Jef Poskanzer <jef@mail.acme.com>
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "libweb.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int argn;
|
||||
char* referer;
|
||||
char* user_agent;
|
||||
char* auth_token;
|
||||
int ncookies;
|
||||
char* cookies[MAX_COOKIES];
|
||||
char* header_name;
|
||||
char* header_value;
|
||||
|
||||
argv0 = argv[0];
|
||||
argn = 1;
|
||||
timeout = 60;
|
||||
referer = (char*) 0;
|
||||
user_agent = "http_post";
|
||||
auth_token = (char*) 0;
|
||||
ncookies = 0;
|
||||
header_name = (char*) 0;
|
||||
header_value = (char*) 0;
|
||||
verbose = 0;
|
||||
|
||||
while(argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') {
|
||||
if(strcmp(argv[argn], "-t") == 0 && argn + 1 < argc) {
|
||||
++argn;
|
||||
timeout = atoi(argv[argn]);
|
||||
} else if(strcmp(argv[argn], "-r") == 0 && argn + 1 < argc) {
|
||||
++argn;
|
||||
referer = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-u") == 0 && argn + 1 < argc) {
|
||||
++argn;
|
||||
user_agent = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-a") == 0 && argn + 1 < argc) {
|
||||
++argn;
|
||||
auth_token = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-c") == 0 && argn + 1 < argc) {
|
||||
if(ncookies >= MAX_COOKIES) {
|
||||
(void) fprintf(stderr, "%s: too many cookies\n", argv0);
|
||||
exit(1);
|
||||
}
|
||||
++argn;
|
||||
cookies[ncookies++] = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-h") == 0 && argn + 2 < argc) {
|
||||
++argn;
|
||||
header_name = argv[argn];
|
||||
++argn;
|
||||
header_value = argv[argn];
|
||||
} else if(strcmp(argv[argn], "-v") == 0) {
|
||||
verbose = 1;
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
++argn;
|
||||
}
|
||||
if(argn >= argc) {
|
||||
usage();
|
||||
}
|
||||
url = argv[argn];
|
||||
++argn;
|
||||
|
||||
(void) signal(SIGALRM, sigcatch);
|
||||
postURL(url, referer, user_agent, auth_token, ncookies, cookies, header_name, header_value, &(argv[argn]), argc - argn);
|
||||
exit(0);
|
||||
}
|
Loading…
Reference in New Issue
Block a user