aboutsummaryrefslogtreecommitdiffstats
path: root/form.cgi
diff options
context:
space:
mode:
authorDennis Eriksen <d@ennis.no>2019-11-01 17:13:40 +0100
committerDennis Eriksen <d@ennis.no>2019-11-01 17:13:40 +0100
commite9d3a1e6796d450ab00a6a6d95b8ace53b7f93fd (patch)
treea33e21079c73694dcced0d8880be5c7227d90a7d /form.cgi
parentJust giving the executables file-endings (diff)
downloadpurl-e9d3a1e6796d450ab00a6a6d95b8ace53b7f93fd.tar.gz
we now support curling the form without getting all of the HTML in response
Diffstat (limited to 'form.cgi')
-rwxr-xr-xform.cgi44
1 files changed, 27 insertions, 17 deletions
diff --git a/form.cgi b/form.cgi
index e10386d..4e3f9d3 100755
--- a/form.cgi
+++ b/form.cgi
@@ -7,17 +7,11 @@ use warnings;
use CGI;
use DBI;
-my $q = CGI->new; # create CGI object
+my $q = CGI->new; # create CGI object
-print $q->header(-charset=>'utf-8');
-print <<HTML;
-<html>
-<head>
- <title>PURL</title>
-</head>
-<body>
-HTML
+my $results;
my $user = scalar $ENV{'REMOTE_USER'};
+my $form = scalar $q->param('form');
# If form has already been submitted
if ($q->param('url')) {
@@ -38,7 +32,7 @@ if ($q->param('url')) {
# Check if short exists. If it does, generate a new one.
while (my $url = $dbh->selectrow_array($query, undef, $short)) {
- print "Your short exists. Generating new.<br>\n";
+ $results .= "Your short exists. Generating new.<br>\n";
$short = genshort();
last;
}
@@ -50,25 +44,41 @@ if ($q->param('url')) {
$dbh->disconnect();
- my $proto = $ENV{'HTTPS'} eq "on" ? 'https://' : 'http://';
+ my $proto = $ENV{'HTTPS'} eq "on" ? 'https://' : 'http://'; # http/https
my $shortURL = $proto . $ENV{'SERVER_NAME'} . '/' . $short;
- print "URL: $url<br>\n";
- print "Shortened to: <a href=\"$shortURL\">$shortURL</a><br>\n";
- print "<hr>\n";
+ $results .= "URL: $url<br>\n";
+ $results .= "Shortened to: <a href=\"$shortURL\">$shortURL</a><br>\n";
+ $results .= "<hr>\n";
+
+ # Change results if we are not submitting via the form
+ $results = $form eq "html" ? $results : $shortURL . "\n";
}
-# print form
-print <<FORM;
+if ($form ne "html" and $results ne "") { # if we did not use the form, and there are no results
+ print $q->header(-charset=>'utf-8');
+ print $results;
+} else { # if we used the form
+ # print form
+ print $q->header(-charset=>'utf-8');
+ print <<HTML;
+<html>
+<head>
+ <title>PURL</title>
+</head>
+<body>
+ $results
<form method="post" action="/purl.cgi" enctype="multipart/form-data" name="main">
Username: $user<br>
URL to shorten: <input type="text" name="url" size="50"><br>
Custom short: <input type="text" name="short"><br>
+ <input type="hidden" name="form" value="html">
<input type="submit" value="Submit">
</form>
</body>
</html>
-FORM
+HTML
+}
exit(0);