[ Previous ] [ Contents ] [ Index ] [ Next ]

Ns_ConnReturnProxyNotice

Overview

Return a notice to the client in proxy modules

Syntax

    EXTERN int Ns_ConnReturnProxyNotice(
    Ns_Conn  *conn,
    int       status,
    char     *notice,
    char     *html
    );

Description

The Ns_ConnReturnProxyNotice function returns a simple HTML page to the client with the specified notice as the title of the page.  The page includes the image at the top of the page, including the hostname and port number in the URL, in the form:

<img src="http://hostname:port/ns/asset/notice.gif">

If the html parameter is not NULL, it is added to the page after the notice.  The HTML source can be arbitrarily long and should not contain the <HTML> or <BODY> begin or end tags; these tags will be added by Ns_ConnReturnProxyNotice.

Ns_ConnReturnProxyNotice returns a status of NS_OK or NS_ERROR.

Ns_ConnReturnProxyNotice is intended to be used in proxy modules, and is in the latest version of the nsproxy.c example (below).

Example

    /* This is a very basic http proxy module that demonstrates the 
       use of Ns_RegisterProxyRequest. Note that this module supports
       only the GET method because it uses Ns_FetchURL. */
       
    #include "ns.h"
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    DllExport int Ns_ModuleVersion = 1; 
    int http_proxy(void *context, Ns_Conn *conn);
    int http_proxy(void *context, Ns_Conn *conn)
    {
      Ns_DString      ds, data;
      int counter;
      char *c;
      Ns_Set *headers;
      unsigned int port = conn->request->port;
      headers = Ns_SetCreate("headers"); 
      
      if (port == 0) {
        port=80;
      }
      Ns_DStringInit(&ds);
      Ns_DStringInit(&data);
      if (!conn->request->query || strlen(conn->request->query)==0) 
        /* build a URL not including a GET style query */
        Ns_DStringPrintf(&data, "%s://%s:%d%s", 
    		     conn->request->protocol,
    		     conn->request->host,
    		     port,
    		     conn->request->url);
      else
        /* build a URL with a GET style query */
        Ns_DStringPrintf(&data, "%s://%s:%d%s?%s", 
    		     conn->request->protocol,
    		     conn->request->host,
    		     port,
    		     conn->request->url,
    		     conn->request->query);
      Ns_Log(Notice, "Proxying: GET %s", data);
      /* The the web page from wherever */
      if (Ns_FetchURL(&ds, Ns_DStringValue((&data)), headers) == 
NS_OK) {
        /* Output what was returned...
         * Ns_FetchURL returns the status line as the new name
         * of the headers set it is passed, so output that first. 
         */
        Ns_ConnWrite(conn, Ns_SetName(headers),
          strlen(Ns_SetName(headers)));
        /* Now iterate through the headers structure and print out 
         * all of theoutput headers 
         */
        for (counter = 0; counter < Ns_SetSize(headers); counter++) {
          c = Ns_SetKey(headers, counter);
          Ns_ConnWrite(conn, c, strlen(c));
          Ns_ConnWrite(conn, ": ", 2);
          c = Ns_SetValue(headers, counter); 
    /* This has a newline in it already*/
          Ns_ConnWrite(conn, c, strlen(c)); 
     /* so no need to add one myself */
        }
        Ns_ConnWrite(conn, "\r\n", 2); 
    /* This blank means no more headers */
        /* Now spit out the HTML... */
        Ns_ConnWrite(conn, Ns_DStringValue((&ds)), 
Ns_DStringLength((&ds)));
      } else {
        Ns_ConnReturnProxyNotice(conn, 400, "Could not proxy", "An 
error was encountered while attempting to proxy your request.");
      }
      Ns_DStringFree(&data);
      Ns_DStringFree(&ds);
      return NS_OK; 
    }
    DllExport int Ns_ModuleInit (char * hServer, char *hModule) {
      Ns_RegisterProxyRequest(hServer, "GET", "http", http_proxy,
        NULL, NULL);
      return NS_OK;
    }

Top of Page

[ Previous ] [ Contents ] [ Index ] [ Next ]
Copyright © 1996 America Online, Inc.