2005-01-19 20:16:09 +03:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is SniffURI.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Erik van der Poel <erik@vanderpoel.org>.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998-2005
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "all.h"
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
static char *me = NULL;
|
|
|
|
|
|
|
|
static char *passThese[] =
|
|
|
|
{
|
|
|
|
"HTTP_USER_AGENT=",
|
|
|
|
"HTTP_ACCEPT=",
|
|
|
|
"HTTP_ACCEPT_CHARSET=",
|
|
|
|
"HTTP_ACCEPT_LANGUAGE=",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTML(App *app, Input *input)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTML(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTMLAttributeName(App *app, HTML *html, Input *input)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTMLAttributeName(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTMLAttributeValue(App *app, HTML *html, Input *input)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-19 20:16:09 +03:00
|
|
|
URL *url;
|
|
|
|
unsigned char *urlstring;
|
2005-01-21 00:09:40 +03:00
|
|
|
View *view;
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
view = &app->view;
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
if (html->currentAttributeIsURL)
|
|
|
|
{
|
|
|
|
url = urlRelative(html->base, html->currentAttribute->value);
|
2005-01-19 20:16:09 +03:00
|
|
|
urlstring = escapeHTML(url ? url->url : (unsigned char *) "");
|
2003-01-02 20:07:34 +03:00
|
|
|
fprintf(view->out, "<a href=\"%s%s\">", me, urlstring);
|
|
|
|
free(urlstring);
|
2000-02-01 21:24:20 +03:00
|
|
|
urlFree(url);
|
|
|
|
}
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTMLAttributeValue(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
if (html->currentAttributeIsURL)
|
|
|
|
{
|
|
|
|
fprintf(view->out, "</a>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTMLTag(App *app, HTML *html, Input *input)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTMLTag(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTMLText(App *app, Input *input)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTMLText(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTTP(App *app, Input *input)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTTP(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTTPBody(App *app, Input *input)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTTP(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTTPHeaderName(App *app, Input *input)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTTPHeaderName(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
static void
|
|
|
|
cgiviewHTTPHeaderValue(App *app, Input *input, unsigned char *url)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-19 20:16:09 +03:00
|
|
|
unsigned char *urlstring;
|
2005-01-21 00:09:40 +03:00
|
|
|
View *view;
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-21 00:09:40 +03:00
|
|
|
view = &app->view;
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
if (url)
|
|
|
|
{
|
2003-01-02 20:07:34 +03:00
|
|
|
urlstring = escapeHTML(url);
|
|
|
|
fprintf(view->out, "<a href=\"%s%s\">", me, urlstring);
|
|
|
|
free(urlstring);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
2005-01-21 00:09:40 +03:00
|
|
|
viewHTTPHeaderValue(app, input);
|
2000-02-01 21:24:20 +03:00
|
|
|
if (url)
|
|
|
|
{
|
|
|
|
fprintf(view->out, "</a>");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char **
|
2005-01-21 00:09:40 +03:00
|
|
|
getHTTPRequestHeaders(App *app, char *host, char *verbose)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
|
|
|
char **e;
|
|
|
|
extern char **environ;
|
|
|
|
int firstLetter;
|
|
|
|
char **h;
|
|
|
|
char *p;
|
|
|
|
char *q;
|
|
|
|
char **r;
|
|
|
|
char **ret;
|
|
|
|
char *scriptName;
|
|
|
|
char *str;
|
|
|
|
|
2000-02-01 22:06:01 +03:00
|
|
|
scriptName = "view.cgi";
|
2000-02-01 21:24:20 +03:00
|
|
|
e = environ;
|
|
|
|
while (*e)
|
|
|
|
{
|
|
|
|
e++;
|
|
|
|
}
|
|
|
|
ret = malloc((e - environ + 1) * sizeof(*e));
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-02-01 22:06:01 +03:00
|
|
|
me = malloc(strlen(scriptName) + strlen(verbose) + 1);
|
2000-02-01 21:24:20 +03:00
|
|
|
if (!me)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-02-01 22:06:01 +03:00
|
|
|
strcpy(me, scriptName);
|
2000-02-01 21:24:20 +03:00
|
|
|
strcat(me, verbose);
|
|
|
|
|
|
|
|
e = environ;
|
|
|
|
r = ret;
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "will send these HTTP Request headers:");
|
2000-02-01 21:24:20 +03:00
|
|
|
while (*e)
|
|
|
|
{
|
|
|
|
h = passThese;
|
|
|
|
while (*h)
|
|
|
|
{
|
|
|
|
if (!strncmp(*e, *h, strlen(*h)))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
h++;
|
|
|
|
}
|
|
|
|
if (*h)
|
|
|
|
{
|
|
|
|
str = malloc(strlen(*e) - 5 + 1 + 1);
|
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
p = *e + 5;
|
|
|
|
q = str;
|
|
|
|
while (*p && (*p != '='))
|
|
|
|
{
|
|
|
|
firstLetter = 1;
|
|
|
|
while (*p && (*p != '=') && (*p != '_'))
|
|
|
|
{
|
|
|
|
if (firstLetter)
|
|
|
|
{
|
|
|
|
*q++ = *p++;
|
|
|
|
firstLetter = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*q++ = tolower(*p);
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p == '_')
|
|
|
|
{
|
|
|
|
*q++ = '-';
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p == '=')
|
|
|
|
{
|
|
|
|
p++;
|
|
|
|
*q++ = ':';
|
|
|
|
*q++ = ' ';
|
|
|
|
while (*p)
|
|
|
|
{
|
|
|
|
*q++ = *p++;
|
|
|
|
}
|
|
|
|
*q = 0;
|
|
|
|
*r++ = str;
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, str);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
e++;
|
|
|
|
}
|
|
|
|
str = malloc(6 + strlen(host) + 1);
|
|
|
|
if (str)
|
|
|
|
{
|
|
|
|
strcpy(str, "Host: ");
|
|
|
|
strcat(str, host);
|
|
|
|
*r++ = str;
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, str);
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReportHTML(app, "<hr>");
|
2000-02-01 21:24:20 +03:00
|
|
|
*r = NULL;
|
|
|
|
|
|
|
|
return (unsigned char **) ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
char *ampersand;
|
2005-01-21 00:09:40 +03:00
|
|
|
App *app;
|
2000-02-01 21:24:20 +03:00
|
|
|
unsigned char *equals;
|
|
|
|
char *name;
|
|
|
|
unsigned char *newURL;
|
|
|
|
char *p;
|
|
|
|
char *query;
|
|
|
|
URL *u;
|
|
|
|
unsigned char *url;
|
|
|
|
char *verbose;
|
|
|
|
View *view;
|
|
|
|
|
2005-01-19 20:16:09 +03:00
|
|
|
if (!netInit())
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!threadInit())
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
url = NULL;
|
|
|
|
|
|
|
|
verbose = "?url=";
|
|
|
|
|
|
|
|
query = getenv("QUERY_STRING");
|
2005-01-21 00:09:40 +03:00
|
|
|
app = appAlloc();
|
|
|
|
app->html = cgiviewHTML;
|
|
|
|
app->htmlAttributeName = cgiviewHTMLAttributeName;
|
|
|
|
app->htmlAttributeValue = cgiviewHTMLAttributeValue;
|
|
|
|
app->htmlTag = cgiviewHTMLTag;
|
|
|
|
app->htmlText = cgiviewHTMLText;
|
|
|
|
app->http = cgiviewHTTP;
|
|
|
|
app->httpBody = cgiviewHTTPBody;
|
|
|
|
app->httpHeaderName = cgiviewHTTPHeaderName;
|
|
|
|
app->httpHeaderValue = cgiviewHTTPHeaderValue;
|
|
|
|
view = &app->view;
|
2000-02-01 21:24:20 +03:00
|
|
|
view->out = stdout;
|
|
|
|
freopen("/dev/null", "w", stderr);
|
|
|
|
fprintf(view->out, "Content-Type: text/html\n");
|
|
|
|
fprintf(view->out, "\n");
|
|
|
|
if (query)
|
|
|
|
{
|
|
|
|
p = query;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
name = p;
|
|
|
|
ampersand = strchr(p, '&');
|
|
|
|
if (ampersand)
|
|
|
|
{
|
|
|
|
*ampersand = 0;
|
|
|
|
p = ampersand + 1;
|
|
|
|
}
|
|
|
|
equals = (unsigned char *) strchr(name, '=');
|
|
|
|
if (equals)
|
|
|
|
{
|
|
|
|
*equals = 0;
|
|
|
|
if (!strcmp(name, "url"))
|
|
|
|
{
|
|
|
|
url = equals + 1;
|
|
|
|
urlDecode(url);
|
|
|
|
}
|
|
|
|
else if (!strcmp(name, "verbose"))
|
|
|
|
{
|
|
|
|
verbose = "?verbose=on&url=";
|
|
|
|
viewVerbose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (ampersand);
|
|
|
|
}
|
|
|
|
else if (argc > 1)
|
|
|
|
{
|
|
|
|
url = (unsigned char *) argv[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(view->out, "no environment variable QUERY_STRING<br>\n");
|
|
|
|
fprintf(view->out, "and no arg passed<br>\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (url && (*url))
|
|
|
|
{
|
|
|
|
fprintf
|
|
|
|
(
|
|
|
|
view->out,
|
|
|
|
"<html><head><title>View %s</title></head>"
|
|
|
|
"<body><tt><b>\n",
|
|
|
|
url
|
|
|
|
);
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "input url:");
|
|
|
|
viewReport(app, (char *) url);
|
|
|
|
viewReportHTML(app, "<hr>");
|
2000-02-01 21:24:20 +03:00
|
|
|
u = urlParse(url);
|
|
|
|
if
|
|
|
|
(
|
|
|
|
((!u->scheme)||(!strcmp((char *) u->scheme, "http"))) &&
|
|
|
|
(!u->host) &&
|
|
|
|
(*url != '/')
|
|
|
|
)
|
|
|
|
{
|
|
|
|
newURL = calloc(strlen((char *) url) + 3, 1);
|
|
|
|
if (!newURL)
|
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "calloc failed");
|
2000-02-01 21:24:20 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
strcpy((char *) newURL, "//");
|
|
|
|
strcat((char *) newURL, (char *) url);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
newURL = copyString(url);
|
|
|
|
}
|
|
|
|
urlFree(u);
|
|
|
|
u = urlParse(newURL);
|
|
|
|
if
|
|
|
|
(
|
|
|
|
(
|
|
|
|
(!u->scheme) ||
|
|
|
|
(!strcmp((char *) u->scheme, "http"))
|
|
|
|
) &&
|
|
|
|
(!*u->path)
|
|
|
|
)
|
|
|
|
{
|
|
|
|
url = newURL;
|
|
|
|
newURL = calloc(strlen((char *) url) + 2, 1);
|
|
|
|
if (!newURL)
|
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "calloc failed");
|
2000-02-01 21:24:20 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
strcpy((char *) newURL, (char *) url);
|
|
|
|
free(url);
|
|
|
|
strcat((char *) newURL, "/");
|
|
|
|
}
|
|
|
|
urlFree(u);
|
|
|
|
u = urlRelative(
|
|
|
|
(unsigned char *) "http://www.mozilla.org/index.html",
|
|
|
|
newURL);
|
|
|
|
free(newURL);
|
2005-01-21 00:09:40 +03:00
|
|
|
viewReport(app, "fully qualified url:");
|
|
|
|
viewReport(app, (char *) u->url);
|
|
|
|
viewReportHTML(app, "<hr>");
|
2000-02-01 21:24:20 +03:00
|
|
|
fflush(view->out);
|
|
|
|
if (!strcmp((char *) u->scheme, "http"))
|
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
httpProcess(app, u,
|
|
|
|
getHTTPRequestHeaders(app, (char *) u->host,
|
2000-02-01 21:24:20 +03:00
|
|
|
verbose));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf
|
|
|
|
(
|
|
|
|
view->out,
|
|
|
|
"Sorry, %s URLs are not supported yet. "
|
|
|
|
"Only http URLs are supported.",
|
|
|
|
u->scheme
|
|
|
|
);
|
|
|
|
}
|
|
|
|
fprintf(view->out, "</b></tt></body></html>\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-01-20 04:38:31 +03:00
|
|
|
fprintf(view->out, "<html><head><title>SniffURI Error</title>");
|
|
|
|
fprintf(view->out, "</head><body><h2>Please enter a URI</h2>");
|
|
|
|
fprintf(view->out, "<a href=index.html>Go Back</a>");
|
|
|
|
fprintf(view->out, "</body></html>");
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
return 1;
|
|
|
|
}
|