home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / cvproxy.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.8 KB  |  132 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. #include "mkutils.h"
  19. #include "mkstream.h"
  20. #include "mkgeturl.h"
  21. #include "xp.h"
  22.  
  23. typedef struct _ProxyObj {
  24.     Bool  past_first_line;
  25.     Bool  definately_send_headers;
  26.     char    *content_type;
  27.     char    *content_encoding;
  28. } ProxyObj;
  29.  
  30. PRIVATE int net_proxy_write (NET_StreamClass *stream, CONST char* s, int32 len)
  31. {
  32.     ProxyObj *obj=stream->data_object;
  33.     /* send HTTP headers if not already getting an HTTP doc
  34.      */    
  35.     if(!obj->past_first_line)
  36.       {
  37.         if(obj->definately_send_headers || strncmp(s, "HTTP/", len >= 5 ? 5 : len))
  38.           {
  39.             write(1, "HTTP/1.0 200 OK\r\n",17);
  40.             write(1, "Content-type: ",14);
  41.             write(1, obj->content_type, XP_STRLEN(obj->content_type));
  42.             if(obj->content_encoding)
  43.               {
  44.                 write(1, "\r\nContent-encoding: ",18);
  45.                 write(1, obj->content_encoding, XP_STRLEN(obj->content_encoding));
  46.               }
  47.             write(1, "\r\nServer: MKLib proxy agent\r\n",29);
  48.             write(1, "\r\n", 2);  /* finish it */
  49.           }
  50.  
  51.         obj->past_first_line = TRUE;
  52.       }
  53.  
  54.     write(1, s, len); 
  55.     return(1);
  56. }
  57.  
  58. PRIVATE unsigned int net_proxy_WriteReady (NET_StreamClass *stream)
  59. {    
  60.    return(MAX_WRITE_READY);  /* always ready for writing */
  61. }
  62.  
  63.  
  64. PRIVATE void net_proxy_complete (NET_StreamClass *stream)
  65. {
  66.     ProxyObj *obj=stream->data_object;    
  67.     FREEIF(obj->content_type);
  68.     FREEIF(obj->content_encoding);
  69.     FREE(obj);
  70.     return;
  71. }
  72.  
  73. PRIVATE void net_proxy_abort (NET_StreamClass *stream, int status)
  74. {
  75.     ProxyObj *obj=stream->data_object;    
  76.     FREEIF(obj->content_type);
  77.     FREEIF(obj->content_encoding);
  78.     FREE(obj);
  79.     return;
  80. }
  81.  
  82.  
  83.  
  84. PUBLIC NET_StreamClass * 
  85. NET_ProxyConverter(int         format_out,
  86.                    void       *data_obj,
  87.                    URL_Struct *URL_s,
  88.                    MWContext  *window_id)
  89. {
  90.     NET_StreamClass* stream;
  91.     ProxyObj * obj;
  92.     
  93.     TRACEMSG(("Setting up display stream. Have URL: %s \n%s\n", 
  94.                                     URL_s->address, URL_s->content_type));
  95.  
  96.     stream = XP_NEW(NET_StreamClass);
  97.     if(stream == NULL) 
  98.         return(NULL);
  99.  
  100.     obj = XP_NEW(ProxyObj);
  101.     if(obj == NULL) 
  102.       {
  103.         FREE(stream);
  104.         return(NULL);
  105.       }
  106.  
  107.     XP_MEMSET(obj, 0, sizeof(ProxyObj));
  108.  
  109.     stream->data_object = obj;
  110.     
  111.  
  112.     stream->name           = "ProxyWriter";
  113.     stream->complete       = (MKStreamCompleteFunc) net_proxy_complete;
  114.     stream->abort          = (MKStreamAbortFunc) net_proxy_abort;
  115.     stream->put_block      = (MKStreamWriteFunc) net_proxy_write;
  116.     stream->is_write_ready = (MKStreamWriteReadyFunc) net_proxy_WriteReady;
  117.     stream->window_id      = window_id;
  118.  
  119.     TRACEMSG(("Returning stream from display_converter\n"));
  120.  
  121.     /* send HTTP headers if not already getting an HTTP doc 
  122.      */
  123.     if(strncasecomp(URL_s->address,"http:",5))
  124.       {
  125.         obj->definately_send_headers = TRUE;
  126.       }
  127.     StrAllocCopy(obj->content_type, URL_s->content_type);
  128.     StrAllocCopy(obj->content_encoding, URL_s->content_encoding);
  129.  
  130.     return stream;
  131. }
  132.