Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions h2c
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/perl

use strict;
use warnings;

use MIME::Base64;

sub usage {
Expand Down Expand Up @@ -30,6 +33,9 @@ my $shellcompatible = 1; # may not been windows command prompt compat
my $uselongoptions = 1; # instead of short
my $uselibcurl = 0; # --libcurl
my $usehttp = 0;
my $usedocs;
my $usenotes;
my $useverbose = "";

while($ARGV[0]) {
if(($ARGV[0] eq "-h") || ($ARGV[0] eq "--help")) {
Expand Down Expand Up @@ -75,6 +81,15 @@ while($ARGV[0]) {

my $state; # 0 is request-line, 1-headers, 2-body
my $line = 1;
my $error;
my $exact;
my $http;
my $method;
my $path;
my @body;
my %exactcase;
my %header;

while(<STDIN>) {
my $l = $_;
# discard CRs completely
Expand Down Expand Up @@ -125,6 +140,16 @@ if($error) {
exit;
}

my $opt_data = "-d";
my $opt_request = "-X";
my $opt_head = "-I";
my $opt_header = "-H";
my $opt_user_agent = "-A";
my $opt_cookie = "-b";
my $opt_verbose = "-v";
my $opt_form = "-F";
my $opt_user = "-u";

if($uselongoptions) {
$opt_data = "--data";
$opt_request = "--request";
Expand All @@ -136,21 +161,18 @@ if($uselongoptions) {
$opt_form = "--form";
$opt_user = "--user";
}
else {
$opt_data = "-d";
$opt_request = "-X";
$opt_head = "-I";
$opt_header = "-H";
$opt_user_agent = "-A";
$opt_cookie = "-b";
$opt_verbose = "-v";
$opt_form = "-F";
$opt_user = "-u";
}

my $httpver="";
my $disabledheaders="";
my $addedheaders="";
my $do_multipart;
my $ignore_contenttype;
my $multipart="";
my $requesttarget="";
my $unixescaped;
my $usebody;
my @docs;
my @notes;

if($header{"content-type"} =~ /^multipart\/form-data;/) {
# multipart formpost, this is special
Expand All @@ -169,6 +191,7 @@ if($header{"content-type"} =~ /^multipart\/form-data;/) {
my %fheader;
my $fstate = 0;
my @fbody;

while($body[$bline]) {
my $l = $body[$bline];
if(0 == $fstate) {
Expand Down Expand Up @@ -248,6 +271,9 @@ elsif(length(join("", @body))) {
$usebody= sprintf("--data-binary \"%s\" ", $esc);
push @docs, manpage("--data-binary", "", "send this string as a body with POST");
}

my $usemethod = "";

if(uc($method) eq "HEAD") {
$usemethod = "$opt_head ";
push @docs, manpage("-I", $opt_head, "send a HEAD request");
Expand Down Expand Up @@ -377,6 +403,8 @@ foreach my $h (sort keys %header) {
}
}

my $url;

if($path =~ /[&?]/) {
$url = sprintf "\"%s://%s%s\"", $usehttp ? "http" : "https", $header{'host'}, $path;
}
Expand All @@ -397,19 +425,20 @@ if($useverbose) {

# This adds the -x option to prevent a curl request to actually go out to any
# remote server
my $lib="--libcurl - -x localhost:0 " if($uselibcurl);
my $lib="";
$lib="--libcurl - -x localhost:0 " if($uselibcurl);
my $curlcmd = "curl ${useverbose}${usemethod}${httpver}${disabledheaders}${addedheaders}${usebody}${multipart}${requesttarget}${lib}${url}";

if($uselibcurl) {
# this actually runs curl which will fail to connect so ignore errors
open(C, "$curlcmd 2>/dev/null|");
while(<C>) {
open(my $c,q{-|},"$curlcmd 2>/dev/null");
while(<$c>) {
# skip CURLOPT_PROXY since that's only used to avoid network
if($_ !~ /CURLOPT_PROXY, /) {
print $_;
}
}
close(C);
close($c);
}
else {
print "$curlcmd\n";
Expand Down