home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / jsautocf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.2 KB  |  304 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"    /* LF */
  19.  
  20. #include <time.h>
  21.  
  22. #include "jsautocf.h"
  23. #include "xp_mem.h"    /* XP_NEW_ZAP() */
  24. #ifndef XP_MAC
  25. #include <sys/types.h>
  26. #endif
  27. #include "libi18n.h"
  28.  
  29. #include "mkstream.h"
  30. #include "mkgeturl.h"
  31. #include "cvextcon.h"
  32. #include "mkformat.h"
  33. /* acharya: This file is obsolete and not used in this file.
  34. #include "il.h" 
  35. */
  36. #include "mime.h"
  37. #include "cvactive.h"
  38. #include "gui.h"
  39. #include "msgcom.h"
  40.  
  41. #include "xp_reg.h"
  42. #if defined(XP_UNIX) || defined(XP_WIN32)
  43. #include "prnetdb.h"
  44. #else
  45. #define PRHostEnt struct hostent
  46. #endif
  47.  
  48. /*
  49.  * Private object for the proxy config loader stream.
  50.  *
  51.  *
  52.  */
  53. typedef struct _JSACF_Object {
  54.    MWContext *    context;
  55.    int        flag;
  56. } JSACF_Object;
  57.  
  58.  
  59. /*
  60.  * A struct for holding queued state.
  61.  *
  62.  * The state is saved when NET_GetURL() is called for the first time,
  63.  * and the Javascript autoconfig retrieve has to be started (and finished)
  64.  * before the first actual document can be loaded.
  65.  *
  66.  * A pre-exit function for the Javascript autoconfig URL restarts the
  67.  * original URL retrieve by calling NET_GetURL() again with the
  68.  * same parameters.
  69.  *
  70.  */
  71. typedef struct _JSACF_QueuedState {
  72.     URL_Struct *    URL_s;
  73.     FO_Present_Types    output_format;
  74.     MWContext *        window_id;
  75.     Net_GetUrlExitFunc*    exit_routine;
  76. } JSACF_QueuedState;
  77.  
  78. PRIVATE Bool    jsacf_loading      = FALSE;
  79. PRIVATE Bool    jsacf_ok           = FALSE;
  80. PRIVATE char *    jsacf_url          = NULL;
  81. PRIVATE char *    jsacf_src_buf      = NULL;
  82. PRIVATE int    jsacf_src_len      = 0;
  83.  
  84.  
  85. /*
  86.  * Saves out the proxy autoconfig file to disk, in case the server
  87.  * is down the next time.
  88.  *
  89.  * Returns 0 on success, -1 on failure.
  90.  *
  91.  */
  92. PRIVATE int jsacf_save_config(void)
  93. {
  94.     XP_File fp;
  95.     int32 len = 0;
  96.  
  97.     // TODO: jonm
  98.     if(!(fp = XP_FileOpen("", xpProxyConfig, XP_FILE_WRITE)))
  99.     return -1;
  100.  
  101.     len = XP_FileWrite(jsacf_src_buf, jsacf_src_len, fp);
  102.     XP_FileClose(fp);
  103.     if (len != jsacf_src_len)
  104.         return -1;
  105.  
  106.     return 0;
  107. }
  108.  
  109.  
  110. /*
  111.  * Reads the proxy autoconfig file from disk.
  112.  * This is called if the config server is not responding.
  113.  *
  114.  * returns 0 on success -1 on failure.
  115.  *
  116.  */
  117. PRIVATE int jsacf_read_config(void)
  118. {
  119.     XP_StatStruct st;
  120.     XP_File fp;
  121.  
  122.     if (XP_Stat("", &st, xpProxyConfig) == -1)
  123.     return -1;
  124.  
  125.     if (!(fp = XP_FileOpen("", xpProxyConfig, XP_FILE_READ)))
  126.     return -1;
  127.  
  128.     jsacf_src_len = st.st_size;
  129.     jsacf_src_buf = (char *)XP_ALLOC(jsacf_src_len + 1);
  130.     if (!jsacf_src_buf) {
  131.     XP_FileClose(fp);
  132.     jsacf_src_len = 0;
  133.     return -1;
  134.     }
  135.  
  136.     if ((jsacf_src_len = XP_FileRead(jsacf_src_buf, jsacf_src_len, fp)) > 0)
  137.       {
  138.       jsacf_src_buf[jsacf_src_len] = '\0';
  139.       }
  140.     else
  141.       {
  142.       XP_FREE(jsacf_src_buf);
  143.       jsacf_src_buf = NULL;
  144.       jsacf_src_len = 0;
  145.       }
  146.  
  147.     XP_FileClose(fp);
  148.  
  149.     return 0;
  150. }
  151.  
  152.  
  153.  
  154. /*
  155.  * Private stream object methods for receiving the proxy autoconfig
  156.  * file.
  157.  *
  158.  *
  159.  */
  160. PRIVATE int jsacf_write(NET_StreamClass *stream, CONST char *buf, int32 len)
  161. {
  162.     JSACF_Object *obj=stream->data_object;    
  163.     if (len > 0) {
  164.     if (!jsacf_src_buf)
  165.         jsacf_src_buf = (char*)XP_ALLOC(len + 1);
  166.     else
  167.         jsacf_src_buf = (char*)XP_REALLOC(jsacf_src_buf,
  168.                          jsacf_src_len + len + 1);
  169.  
  170.     if (!jsacf_src_buf) {    /* Out of memory */
  171.         jsacf_src_len = 0;
  172.         return MK_DATA_LOADED;
  173.     }
  174.  
  175.     XP_MEMCPY(jsacf_src_buf + jsacf_src_len, buf, len);
  176.     jsacf_src_len += len;
  177.     jsacf_src_buf[jsacf_src_len] = '\0';
  178.     }
  179.     return MK_DATA_LOADED;
  180. }
  181.  
  182.  
  183. PRIVATE unsigned int jsacf_write_ready(NET_StreamClass *stream)
  184. {    
  185.     return MAX_WRITE_READY;
  186. }
  187.  
  188. extern int PREF_EvaluateJSBuffer(char * js_buffer, size_t length);
  189.  
  190. PRIVATE void jsacf_complete(NET_StreamClass *stream)
  191. {
  192.     JSACF_Object *obj=stream->data_object;
  193.     int err = PREF_EvaluateJSBuffer(jsacf_src_buf,jsacf_src_len);    
  194.     if (jsacf_src_buf) XP_FREE(jsacf_src_buf);
  195. }
  196.  
  197.  
  198. PRIVATE void jsacf_abort(NET_StreamClass *stream, int status)
  199. {
  200.     JSACF_Object *obj=stream->data_object;    
  201.     jsacf_loading = FALSE;
  202. //    FE_Alert(obj->context, CONFIG_LOAD_ABORTED);
  203.     XP_FREE(obj);
  204. }
  205.  
  206. /*
  207.  * A stream constructor function for application/x-ns-proxy-autoconfig.
  208.  *
  209.  *
  210.  *
  211.  */
  212. MODULE_PRIVATE NET_StreamClass *
  213. NET_JavaScriptAutoConfig(int fmt, void *data_obj, URL_Struct *URL_s, MWContext *w)
  214. {
  215.     JSACF_Object        *obj;
  216.     NET_StreamClass    *stream;
  217.  
  218. #ifdef LATER
  219.     if (!jsacf_loading) {
  220.     /*
  221.      * The Navigator didn't start this config retrieve
  222.      * intentionally.  Discarding the config.
  223.      */
  224.     alert2(w, CONFIG_BLAST_WARNING, URL_s->address);
  225.     return NULL;
  226.     }
  227.     else {
  228.     NET_Progress(w, XP_GetString( XP_RECEIVING_PROXY_AUTOCFG ) ); 
  229.     }
  230. #endif
  231.  
  232.     if (jsacf_src_buf) {
  233.     XP_FREE(jsacf_src_buf);
  234.     jsacf_src_buf = NULL;
  235.     jsacf_src_len = 0;
  236.     }
  237.  
  238.     if (!(stream = XP_NEW_ZAP(NET_StreamClass)))
  239.     return NULL;
  240.  
  241.     if (!(obj = XP_NEW_ZAP(JSACF_Object))) {
  242.     XP_FREE(stream);
  243.     return NULL;
  244.     }
  245.  
  246.     obj->context = w;
  247.  
  248.     stream->data_object        = obj;
  249.     stream->name        = "JavaScriptAutoConfigLoader";
  250.     stream->complete        = (MKStreamCompleteFunc)  jsacf_complete;
  251.     stream->abort        = (MKStreamAbortFunc)     jsacf_abort;
  252.     stream->put_block        = (MKStreamWriteFunc)     jsacf_write;
  253.     stream->is_write_ready    = (MKStreamWriteReadyFunc)jsacf_write_ready;
  254.     stream->window_id        = w;
  255.  
  256.     return stream;
  257. }
  258.  
  259.  
  260. void jsacf_exit_routine(URL_Struct *URL_s, int status, MWContext *window_id)
  261. {}
  262.  
  263. PRIVATE void
  264. simple_exit(URL_Struct *URL_s, int status, MWContext *window_id)
  265. {
  266.     if(status != MK_CHANGING_CONTEXT)
  267.         NET_FreeURLStruct(URL_s);
  268. }
  269.  
  270. /*
  271.  * Called by mkgeturl.c to originally retrieve, and re-retrieve
  272.  * the javascript autoconfig file.
  273.  *
  274.  * autoconfig_url is the URL pointing to the autoconfig.
  275.  *
  276.  */
  277. MODULE_PRIVATE int NET_LoadJavaScriptConfig(char *autoconf_url,MWContext *window_id)
  278. {
  279.     URL_Struct *my_url_s = NULL;
  280.  
  281.     if (!autoconf_url)
  282.     return -1;
  283.  
  284.     StrAllocCopy(jsacf_url, autoconf_url);
  285.     my_url_s = NET_CreateURLStruct(autoconf_url, NET_SUPER_RELOAD);
  286.  
  287.     /* Alert the proxy autoconfig module that config is coming */
  288.     jsacf_loading = TRUE;
  289.  
  290.     return NET_GetURL(my_url_s, FO_CACHE_AND_JAVASCRIPT_CONFIG, window_id, simple_exit);
  291.     //return FE_GetURL(window_id, my_url_s);
  292. }
  293.  
  294. /*
  295.  * Returns a pointer to a NULL-terminted buffer which contains
  296.  * the text of the proxy autoconfig file.
  297.  *
  298.  *
  299.  */
  300. PUBLIC char * NET_GetJavaScriptConfigSource(void)
  301. {
  302.     return jsacf_src_buf;
  303. }
  304.