1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/*
* The contents of this file are subject to the AOLserver 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://aolserver.com/.
*
* 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 AOLserver Code and related documentation
* distributed by AOL.
*
* The Initial Developer of the Original Code is America Online,
* Inc. Portions created by AOL are Copyright (C) 1999 America Online,
* Inc. All Rights Reserved.
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU General Public License (the "GPL"), in which case the
* provisions of GPL are applicable instead of those above. If you wish
* to allow use of your version of this file only under the terms of the
* GPL and not to allow others to use your version of this file under the
* License, indicate your decision by deleting the provisions above and
* replace them with the notice and other provisions required by the GPL.
* If you do not delete the provisions above, a recipient may use your
* version of this file under either the License or the GPL.
*/
/*
* urlopen.c --
*
* Make outgoing HTTP requests.
*/
static const char *RCSID = "@(#) $Header: /cvsroot/aolserver/aolserver/nsd/urlopen.c,v 1.16 2003/02/04 23:10:51 jrasmuss23 Exp $, compiled: " __DATE__ " " __TIME__;
#include "nsd.h"
#define BUFSIZE 2048
typedef struct Stream {
SOCKET sock;
int error;
int cnt;
char *ptr;
char buf[BUFSIZE+1];
} Stream;
/*
* Local functions defined in this file
*/
static int GetLine(Stream *sPtr, Ns_DString *dsPtr);
static int FillBuf(Stream *sPtr);
/*
*----------------------------------------------------------------------
*
* Ns_FetchPage --
*
* Fetch a page off of this very server. Url must reference a
* file in the filesystem.
*
* Results:
* NS_OK or NS_ERROR.
*
* Side effects:
* The file contents will be put into the passed-in dstring.
*
*----------------------------------------------------------------------
*/
int
Ns_FetchPage(Ns_DString *dsPtr, char *url, char *server)
{
Ns_DString path;
int fd;
int nread;
char buf[1024];
Ns_DStringInit(&path);
Ns_UrlToFile(&path, server, url);
fd = open(path.string, O_RDONLY|O_BINARY);
Ns_DStringFree(&path);
if (fd < 0) {
return NS_ERROR;
}
while ((nread = read(fd, buf, sizeof(buf))) > 0) {
Ns_DStringNAppend(dsPtr, buf, nread);
}
close(fd);
return NS_OK;
}
/*
*----------------------------------------------------------------------
*
* Ns_FetchURL --
*
* Open up an HTTP connection to an arbitrary URL.
*
* Results:
* NS_OK or NS_ERROR.
*
* Side effects:
* Page contents will be appended to the passed-in dstring.
* Headers returned to us will be put into the passed-in Ns_Set.
* The set name will be changed to a copy of the HTTP status line.
*
*----------------------------------------------------------------------
*/
int
Ns_FetchURL(Ns_DString *dsPtr, char *url, Ns_Set *headers)
{
SOCKET sock;
char *p;
Ns_DString ds;
Stream stream;
Ns_Request *request;
int status, tosend, n;
status = NS_ERROR;
sock = INVALID_SOCKET;
Ns_DStringInit(&ds);
/*
* Parse the URL and open a connection.
*/
Ns_DStringVarAppend(&ds, "GET ", url, " HTTP/1.0", NULL);
request = Ns_ParseRequest(ds.string);
if (request == NULL || request->protocol == NULL ||
!STREQ(request->protocol, "http") || request->host == NULL) {
Ns_Log(Notice, "urlopen: invalid url '%s'", url);
goto done;
}
if (request->port == 0) {
request->port = 80;
}
sock = Ns_SockConnect(request->host, request->port);
if (sock == INVALID_SOCKET) {
Ns_Log(Error, "urlopen: failed to connect to '%s': '%s'",
url, ns_sockstrerror(ns_sockerrno));
goto done;
}
/*
* Send a simple HTTP GET request.
*/
Ns_DStringTrunc(&ds, 0);
Ns_DStringVarAppend(&ds, "GET ", request->url, NULL);
if (request->query != NULL) {
Ns_DStringVarAppend(&ds, "?", request->query, NULL);
}
Ns_DStringAppend(&ds, " HTTP/1.0\r\nAccept: */*\r\n\r\n");
p = ds.string;
tosend = ds.length;
while (tosend > 0) {
n = send(sock, p, tosend, 0);
if (n == SOCKET_ERROR) {
Ns_Log(Error, "urlopen: failed to send data to '%s': '%s'",
url, ns_sockstrerror(ns_sockerrno));
goto done;
}
tosend -= n;
p += n;
}
/*
* Buffer the socket and read the response line and then
* consume the headers, parsing them into any given header set.
*/
stream.cnt = 0;
stream.error = 0;
stream.ptr = stream.buf;
stream.sock = sock;
if (!GetLine(&stream, &ds)) {
goto done;
}
if (headers != NULL && strncmp(ds.string, "HTTP", 4) == 0) {
if (headers->name != NULL) {
ns_free(headers->name);
}
headers->name = Ns_DStringExport(&ds);
}
do {
if (!GetLine(&stream, &ds)) {
goto done;
}
if (ds.length > 0
&& headers != NULL
&& Ns_ParseHeader(headers, ds.string, Preserve) != NS_OK) {
goto done;
}
} while (ds.length > 0);
/*
* Without any check on limit or total size, foolishly read
* the remaining content into the dstring.
*/
do {
Ns_DStringNAppend(dsPtr, stream.ptr, stream.cnt);
} while (FillBuf(&stream));
if (!stream.error) {
status = NS_OK;
}
done:
if (request != NULL) {
Ns_FreeRequest(request);
}
if (sock != INVALID_SOCKET) {
ns_sockclose(sock);
}
Ns_DStringFree(&ds);
return status;
}
/*
*----------------------------------------------------------------------
*
* NsTclGetUrlObjCmd --
*
* Implements ns_geturl.
*
* Results:
* Tcl result.
*
* Side effects:
* See docs.
*
*----------------------------------------------------------------------
*/
int
NsTclGetUrlObjCmd(ClientData arg, Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[])
{
NsInterp *itPtr = arg;
Ns_DString ds;
Ns_Set *headers;
int status, code;
char *url;
if ((objc != 3) && (objc != 2)) {
Tcl_WrongNumArgs(interp, 1, objv, "url ?headersSetIdVar?");
return TCL_ERROR;
}
code = TCL_ERROR;
if (objc == 2) {
headers = NULL;
} else {
headers = Ns_SetCreate(NULL);
}
Ns_DStringInit(&ds);
url = Tcl_GetString(objv[1]);
if (url[1] == '/') {
status = Ns_FetchPage(&ds, Tcl_GetString(objv[1]), itPtr->servPtr->server);
} else {
status = Ns_FetchURL(&ds, Tcl_GetString(objv[1]), headers);
}
if (status != NS_OK) {
Tcl_AppendStringsToObj(Tcl_GetObjResult(interp), "could not fetch: ",
Tcl_GetString(objv[1]), NULL);
if (headers != NULL) {
Ns_SetFree(headers);
}
goto done;
}
if (objc == 3) {
Ns_TclEnterSet(interp, headers, NS_TCL_SET_DYNAMIC);
if (Tcl_ObjSetVar2(interp, objv[2], NULL, Tcl_GetObjResult(interp),
TCL_LEAVE_ERR_MSG) == NULL) {
goto done;
}
}
Tcl_SetResult(interp, ds.string, TCL_VOLATILE);
code = TCL_OK;
done:
Ns_DStringFree(&ds);
return code;
}
/*
*----------------------------------------------------------------------
*
* FillBuf --
*
* Fill the socket stream buffer.
*
* Results:
* 1 if fill ok, 0 otherwise.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
FillBuf(Stream *sPtr)
{
int n;
n = recv(sPtr->sock, sPtr->buf, BUFSIZE, 0);
if (n <= 0) {
if (n < 0) {
Ns_Log(Error, "urlopen: "
"failed to fill socket stream buffer: '%s'", strerror(errno));
sPtr->error = 1;
}
return 0;
}
sPtr->buf[n] = '\0';
sPtr->ptr = sPtr->buf;
sPtr->cnt = n;
return 1;
}
/*
*----------------------------------------------------------------------
*
* GetLine --
*
* Copy the next line from the stream to a dstring, trimming
* the \n and \r.
*
* Results:
* 1 or 0.
*
* Side effects:
* The dstring is truncated on entry.
*
*----------------------------------------------------------------------
*/
static int
GetLine(Stream *sPtr, Ns_DString *dsPtr)
{
char *eol;
int n;
Ns_DStringTrunc(dsPtr, 0);
do {
if (sPtr->cnt > 0) {
eol = strchr(sPtr->ptr, '\n');
if (eol == NULL) {
n = sPtr->cnt;
} else {
*eol++ = '\0';
n = eol - sPtr->ptr;
}
Ns_DStringNAppend(dsPtr, sPtr->ptr, n - 1);
sPtr->ptr += n;
sPtr->cnt -= n;
if (eol != NULL) {
n = dsPtr->length;
if (n > 0 && dsPtr->string[n-1] == '\r') {
Ns_DStringTrunc(dsPtr, n-1);
}
return 1;
}
}
} while (FillBuf(sPtr));
return 0;
}
|