tinyproxy/src/conns.c
Robert James Kaes 0a8e4e4d8d Added reverse proxy support from Kim Holviala. His comments regarding
this addition follow:

  The patch implements a simple reverse proxy (with one funky extra
  feature). It has all the regular features: mapping remote servers to local
  namespace (ReversePath), disabling forward proxying (ReverseOnly) and HTTP
  redirect rewriting (ReverseBaseURL).

  The funky feature is this: You map Google to /google/ and the Google front
  page opens up fine. Type in stuff and click "Google Search" and you'll get
  an error from tinyproxy. Reason for this is that Google's form submits to
  "/search" which unfortunately bypasses our /google/ mapping (if they'd
  submit to "search" without the slash it would have worked ok). Turn on
  ReverseMagic and it starts working....

  ReverseMagic "hijacks" one cookie which it sends to the client browser.
  This cookie contains the current reverse proxy path mapping (in the above
  case /google/) so that even if the site uses absolute links the reverse
  proxy still knows where to map the request.

  And yes, it works. No, I've never seen this done before - I couldn't find
  _any_ working OSS reverse proxies, and the commercial ones I've seen try
  to parse the page and fix all links (in the above case changing "/search"
  to "/google/search"). The problem with modifying the html is that it might
  not be parsable (very common) or it might be encoded so that the proxy
  can't read it (mod_gzip or likes).

  Hope you like that patch. One caveat - I haven't coded with C in like
  three years so my code might be a bit messy.... There shouldn't be any
  security problems thou, but you never know. I did all the stuff out of my
  memory without reading any RFC's, but I tested everything with Moz, Konq,
  IE6, Links and Lynx and they all worked fine.
2004-01-26 19:11:52 +00:00

139 lines
3.3 KiB
C

/* $Id: conns.c,v 1.20 2004-01-26 19:11:51 rjkaes Exp $
*
* Create and free the connection structure. One day there could be
* other connection related tasks put here, but for now the header
* file and this file are only used for create/free functions and the
* connection structure definition.
*
* Copyright (C) 2001 Robert James Kaes (rjkaes@flarenet.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*/
#include "tinyproxy.h"
#include "buffer.h"
#include "conns.h"
#include "heap.h"
#include "log.h"
#include "stats.h"
struct conn_s *
initialize_conn(int client_fd, const char* ipaddr, const char* string_addr)
{
struct conn_s *connptr;
struct buffer_s *cbuffer, *sbuffer;
assert(client_fd >= 0);
/*
* Allocate the memory for all the internal components
*/
cbuffer = new_buffer();
sbuffer = new_buffer();
if (!cbuffer || !sbuffer)
goto error_exit;
/*
* Allocate the space for the conn_s structure itself.
*/
connptr = (struct conn_s*)safemalloc(sizeof(struct conn_s));
if (!connptr)
goto error_exit;
connptr->client_fd = client_fd;
connptr->server_fd = -1;
connptr->cbuffer = cbuffer;
connptr->sbuffer = sbuffer;
connptr->request_line = NULL;
/* These store any error strings */
connptr->error_variables = NULL;
connptr->error_string = NULL;
connptr->error_number = -1;
connptr->connect_method = FALSE;
connptr->show_stats = FALSE;
connptr->protocol.major = connptr->protocol.minor = 0;
/* There is _no_ content length initially */
connptr->content_length.server = connptr->content_length.client = -1;
connptr->client_ip_addr = safestrdup(ipaddr);
connptr->client_string_addr = safestrdup(string_addr);
update_stats(STAT_OPEN);
#ifdef REVERSE_SUPPORT
connptr->reversepath = NULL;
#endif
return connptr;
error_exit:
/*
* If we got here, there was a problem allocating memory
*/
if (cbuffer)
delete_buffer(cbuffer);
if (sbuffer)
delete_buffer(sbuffer);
return NULL;
}
void
destroy_conn(struct conn_s *connptr)
{
assert(connptr != NULL);
if (connptr->client_fd != -1)
if (close(connptr->client_fd) < 0)
log_message(LOG_INFO, "Client (%d) close message: %s",
connptr->client_fd, strerror(errno));
if (connptr->server_fd != -1)
if (close(connptr->server_fd) < 0)
log_message(LOG_INFO, "Server (%d) close message: %s",
connptr->server_fd, strerror(errno));
if (connptr->cbuffer)
delete_buffer(connptr->cbuffer);
if (connptr->sbuffer)
delete_buffer(connptr->sbuffer);
if (connptr->request_line)
safefree(connptr->request_line);
if (connptr->error_variables)
hashmap_delete(connptr->error_variables);
if (connptr->error_string)
safefree(connptr->error_string);
if (connptr->client_ip_addr)
safefree(connptr->client_ip_addr);
if (connptr->client_string_addr)
safefree(connptr->client_string_addr);
#ifdef REVERSE_SUPPORT
if (connptr->reversepath)
safefree(connptr->reversepath);
#endif
safefree(connptr);
update_stats(STAT_CLOSE);
}