home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / mkdaturl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.9 KB  |  157 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.  
  19. #include "mkutils.h"
  20. #include "mkgeturl.h"
  21. #include "cvmime.h"
  22. #include "mkdaturl.h"
  23.  
  24. extern int MK_OUT_OF_MEMORY;
  25. extern int MK_MALFORMED_URL_ERROR;
  26.  
  27. /* format of the DATA: URL
  28.  *
  29.  * data:[CONTENT-TYPE][;base64],DATA
  30.  */
  31. PRIVATE int32
  32. net_DataURLLoad (ActiveEntry * ce)
  33. {
  34.     char *data_buffer;
  35.     XP_Bool is_base64 = FALSE;
  36.     NET_StreamClass *stream;
  37.     char *comma;
  38.  
  39.     ce->protocol = DATA_TYPE_URL;
  40.  
  41.     /* we need a buffer equal to or smaller than the size of the URL
  42.      */
  43.     data_buffer = (char *)XP_ALLOC(XP_STRLEN(ce->URL_s->address)+1);
  44.  
  45.     if(!data_buffer)
  46.         return(MK_OUT_OF_MEMORY);
  47.  
  48.     /* determine the content type */
  49.  
  50.     /* find the first comma */
  51.     comma = XP_STRCHR(ce->URL_s->address, ',');
  52.  
  53.     /* if no comma abort */
  54.     if(!comma)
  55.     {
  56.         ce->URL_s->error_msg = NET_ExplainErrorDetails(MK_MALFORMED_URL_ERROR, ce->URL_s->address);
  57.         return(MK_MALFORMED_URL_ERROR);
  58.     }
  59.  
  60.     /* fill in default content type */
  61.     StrAllocCopy(ce->URL_s->content_type, TEXT_PLAIN);
  62.  
  63.     /* check for a content type */
  64.     if(comma != ce->URL_s->address + XP_STRLEN("data:"))
  65.     {
  66.         *comma = '\0';
  67.         XP_STRCPY(data_buffer, ce->URL_s->address + XP_STRLEN("data:"));        
  68.         *comma = ',';
  69.  
  70.         /* check for base 64 encoding */
  71.         if(strcasestr(data_buffer, "base64"))
  72.             is_base64 = TRUE;
  73.  
  74.         /* parse the rest as a content-type */
  75.         NET_ParseContentTypeHeader(ce->window_id, data_buffer, ce->URL_s, FALSE);
  76.  
  77.     }
  78.  
  79.     if(is_base64)
  80.     {
  81.         stream = NET_MimeEncodingConverter(ce->format_out, ENCODING_BASE64, ce->URL_s, ce->window_id);
  82.     }
  83.     else
  84.     {
  85.         /* open the outgoing stream
  86.          */
  87.         stream = NET_StreamBuilder(ce->format_out, ce->URL_s, ce->window_id);
  88.     }
  89.  
  90.     if(!stream)
  91.       {
  92.         ce->URL_s->error_msg = NET_ExplainErrorDetails(MK_UNABLE_TO_CONVERT);
  93.         ce->status = MK_UNABLE_TO_CONVERT;
  94.         return (ce->status);
  95.       }
  96.  
  97.     /* @@@@ bug: ignore is_write_ready */
  98.  
  99.     /* copy the data part of the URL into a scratch buffer */
  100.     XP_STRCPY(data_buffer, comma+1);
  101.  
  102.     ce->status = (*stream->put_block)(stream,
  103.                                         data_buffer,
  104.                                         XP_STRLEN(data_buffer));
  105.     if(ce->status < 0)
  106.       {
  107.         (*stream->abort)(stream, ce->status);
  108.         return (ce->status);
  109.       }
  110.  
  111.     (*stream->complete)(stream);
  112.  
  113.     ce->status = MK_DATA_LOADED;
  114.     return(-1); /* all done */
  115.     
  116. }
  117.  
  118. /* called repeatedly from NET_ProcessNet to push all the
  119.  * data up the stream
  120.  */
  121. PRIVATE int32
  122. net_ProcessDataURL (ActiveEntry * cur_entry)
  123. {
  124.     XP_ASSERT(0);
  125.     return(-1);
  126. }
  127.  
  128. /* called by functions in mkgeturl to interrupt the loading of
  129.  * an object.  (Usually a user interrupt) 
  130.  */
  131. PRIVATE int32
  132. net_InterruptDataURL (ActiveEntry * cur_entry)
  133. {
  134.     XP_ASSERT(0);
  135.     return(-1);
  136. }
  137.  
  138. PRIVATE void
  139. net_CleanupDataURL(void)
  140. {
  141.  
  142. }
  143.  
  144. MODULE_PRIVATE void
  145. NET_InitDataURLProtocol(void)
  146. {
  147.         static NET_ProtoImpl dataurl_proto_impl;
  148.  
  149.         dataurl_proto_impl.init = net_DataURLLoad;
  150.         dataurl_proto_impl.process = net_ProcessDataURL;
  151.         dataurl_proto_impl.interrupt = net_InterruptDataURL;
  152.         dataurl_proto_impl.cleanup = net_CleanupDataURL;
  153.  
  154.         NET_RegisterProtocolImplementation(&dataurl_proto_impl, DATA_TYPE_URL);
  155. }
  156.  
  157.