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 ***** */
|
2000-02-01 21:24:20 +03:00
|
|
|
|
2005-01-19 20:16:09 +03:00
|
|
|
#include "all.h"
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
static char *limitURLs[] =
|
|
|
|
{
|
2005-01-22 02:54:34 +03:00
|
|
|
NULL,
|
2000-02-01 21:24:20 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static URL *lastURL = NULL;
|
|
|
|
static URL *urls = NULL;
|
|
|
|
|
|
|
|
static void
|
2005-01-21 00:09:40 +03:00
|
|
|
addURLFunc(App *app, URL *url)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
|
|
|
lastURL->next = url;
|
|
|
|
lastURL = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-01-22 02:54:34 +03:00
|
|
|
grab(char *dir, unsigned char *url, HTTP *http)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
|
|
|
char *add;
|
|
|
|
int baseLen;
|
|
|
|
FILE *file;
|
|
|
|
char *p;
|
|
|
|
char *slash;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
baseLen = strlen(limitURLs[0]);
|
|
|
|
if (strncmp((char *) url, limitURLs[0], baseLen))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "no match: %s vs %s\n", url, limitURLs[0]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url[strlen((char *) url) - 1] == '/')
|
|
|
|
{
|
|
|
|
add = "index.html";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
add = "";
|
|
|
|
}
|
|
|
|
|
2005-01-22 02:54:34 +03:00
|
|
|
str = calloc(strlen(dir) + 1 + strlen((char *) url + baseLen) +
|
|
|
|
strlen(add) + 1, 1);
|
2000-02-01 21:24:20 +03:00
|
|
|
if (!str)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "cannot calloc string\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
2005-01-22 02:54:34 +03:00
|
|
|
strcpy(str, dir);
|
|
|
|
strcat(str, "/");
|
|
|
|
strcat(str, (char *) url + baseLen);
|
2000-02-01 21:24:20 +03:00
|
|
|
p = strchr(str, '#');
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
strcat(str, add);
|
|
|
|
p = str;
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
slash = strchr(p, '/');
|
|
|
|
if (!slash)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*slash = 0;
|
|
|
|
if (mkdir(str, 0777))
|
|
|
|
{
|
|
|
|
if (errno != EEXIST)
|
|
|
|
{
|
|
|
|
perror("mkdir");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*slash = '/';
|
|
|
|
p = slash + 1;
|
|
|
|
}
|
|
|
|
file = fopen(str, "w");
|
|
|
|
if (!file)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "cannot open file %s for writing\n", str);
|
|
|
|
exit(0);
|
|
|
|
}
|
2005-01-25 11:22:29 +03:00
|
|
|
bufSetFD(http->in, fileno(file));
|
|
|
|
bufSet(http->in, http->body);
|
|
|
|
bufWrite(http->in);
|
2000-02-01 21:24:20 +03:00
|
|
|
fclose(file);
|
|
|
|
free(str);
|
|
|
|
}
|
|
|
|
|
2005-01-22 02:54:34 +03:00
|
|
|
static void
|
|
|
|
usage(char *prog)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s [ -d dir ] [ -u uri ]\n", prog);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2000-02-01 21:24:20 +03:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2005-01-22 02:54:34 +03:00
|
|
|
char *dir;
|
2000-02-01 21:24:20 +03:00
|
|
|
HTTP *http;
|
2005-01-22 02:54:34 +03:00
|
|
|
int i;
|
2000-02-01 21:24:20 +03:00
|
|
|
char *prog;
|
|
|
|
URL *url;
|
|
|
|
|
2005-01-19 20:16:09 +03:00
|
|
|
if (!netInit())
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!threadInit())
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2000-02-01 21:24:20 +03:00
|
|
|
|
|
|
|
prog = strrchr(argv[0], '/');
|
|
|
|
if (prog)
|
|
|
|
{
|
|
|
|
prog++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
prog = argv[0];
|
|
|
|
}
|
|
|
|
|
2005-01-22 02:54:34 +03:00
|
|
|
dir = NULL;
|
|
|
|
for (i = 1; i < argc; i++)
|
2000-02-01 21:24:20 +03:00
|
|
|
{
|
2005-01-22 02:54:34 +03:00
|
|
|
if (!strcmp(argv[i], "-d"))
|
|
|
|
{
|
|
|
|
if ((++i < argc) && (!dir))
|
|
|
|
{
|
|
|
|
dir = argv[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usage(prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp(argv[i], "-u"))
|
|
|
|
{
|
|
|
|
if ((++i < argc) && (!limitURLs[0]))
|
|
|
|
{
|
|
|
|
limitURLs[0] = argv[i];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usage(prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
usage(prog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!dir)
|
|
|
|
{
|
|
|
|
dir = "test/grab";
|
|
|
|
}
|
|
|
|
if (!limitURLs[0])
|
|
|
|
{
|
|
|
|
limitURLs[0] = "http://sniffuri.org/";
|
2000-02-01 21:24:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
addURLInit(addURLFunc, limitURLs, NULL);
|
|
|
|
|
|
|
|
url = urlParse((unsigned char *) limitURLs[0]);
|
|
|
|
urls = url;
|
|
|
|
lastURL = url;
|
|
|
|
while (url)
|
|
|
|
{
|
2005-01-21 00:09:40 +03:00
|
|
|
appDefault.data = url;
|
2005-01-25 11:22:29 +03:00
|
|
|
http = httpProcess(&appDefault, url, NULL, NULL);
|
2000-02-01 21:24:20 +03:00
|
|
|
if (http)
|
|
|
|
{
|
|
|
|
switch (http->status)
|
|
|
|
{
|
|
|
|
case 200: /* OK */
|
2005-01-22 02:54:34 +03:00
|
|
|
grab(dir, url->url, http);
|
2000-02-01 21:24:20 +03:00
|
|
|
break;
|
|
|
|
case 302: /* Moved Temporarily */
|
|
|
|
break;
|
|
|
|
case 403: /* Forbidden */
|
|
|
|
break;
|
|
|
|
case 404: /* Not Found */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
printf("status %d\n", http->status);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
httpFree(http);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("httpProcess failed: %s\n", url->url);
|
|
|
|
}
|
|
|
|
url = url->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|