2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Retrieve An Image Via URL. %
21 % Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization %
22 % dedicated to making software imaging solutions freely available. %
24 % You may not use this file except in compliance with the License. You may %
25 % obtain a copy of the License at %
27 % http://www.imagemagick.org/script/license.php %
29 % Unless required by applicable law or agreed to in writing, software %
30 % distributed under the License is distributed on an "AS IS" BASIS, %
31 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
32 % See the License for the specific language governing permissions and %
33 % limitations under the License. %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/blob.h"
45 #include "magick/blob-private.h"
46 #include "magick/constitute.h"
47 #include "magick/exception.h"
48 #include "magick/exception-private.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/list.h"
52 #include "magick/magick.h"
53 #include "magick/memory_.h"
54 #include "magick/quantum-private.h"
55 #include "magick/static.h"
56 #include "magick/resource_.h"
57 #include "magick/string_.h"
58 #include "magick/module.h"
59 #if defined(MAGICKCORE_XML_DELEGATE)
60 # if defined(MAGICKCORE_WINDOWS_SUPPORT)
61 # if defined(__MINGW32__)
64 # include <win32config.h>
67 # include <libxml/parser.h>
68 # include <libxml/xmlmemory.h>
69 # include <libxml/nanoftp.h>
70 # include <libxml/nanohttp.h>
74 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
78 % R e a d U R L I m a g e %
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 % ReadURLImage retrieves an image via URL, decodes the image, and returns
85 % it. It allocates the memory necessary for the new Image structure and
86 % returns a pointer to the new image.
88 % The format of the ReadURLImage method is:
90 % Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
92 % A description of each parameter follows:
94 % o image_info: the image info.
96 % o exception: return any errors or warnings in this structure.
100 #if defined(__cplusplus) || defined(c_plusplus)
104 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
105 static void GetFTPData(void *userdata,const char *data,int size)
113 file=(FILE *) userdata;
114 if (file == (FILE *) NULL)
118 length=fwrite(data,size,1,file);
122 #if defined(__cplusplus) || defined(c_plusplus)
126 static Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
128 #define MaxBufferExtent 8192
131 filename[MaxTextExtent];
145 image=(Image *) NULL;
146 read_info=CloneImageInfo(image_info);
147 SetImageInfoBlob(read_info,(void *) NULL,0);
149 unique_file=AcquireUniqueFileResource(read_info->filename);
150 if (unique_file != -1)
151 file=fdopen(unique_file,"wb");
152 if ((unique_file == -1) || (file == (FILE *) NULL))
154 read_info=DestroyImageInfo(read_info);
155 (void) CopyMagickString(image->filename,read_info->filename,
157 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
159 image=DestroyImageList(image);
160 return((Image *) NULL);
162 (void) CopyMagickString(filename,image_info->magick,MaxTextExtent);
163 (void) ConcatenateMagickString(filename,":",MaxTextExtent);
164 LocaleLower(filename);
165 (void) ConcatenateMagickString(filename,image_info->filename,MaxTextExtent);
166 if (LocaleCompare(read_info->magick,"file") == 0)
168 (void) RelinquishUniqueFileResource(read_info->filename);
170 (void) CopyMagickString(read_info->filename,image_info->filename+2,
173 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
174 if (LocaleCompare(read_info->magick,"ftp") == 0)
180 context=xmlNanoFTPNewCtxt(filename);
181 if (context != (void *) NULL)
183 if (xmlNanoFTPConnect(context) >= 0)
184 (void) xmlNanoFTPGet(context,GetFTPData,(void *) file,
186 (void) xmlNanoFTPClose(context);
190 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
191 if (LocaleCompare(read_info->magick,"http") == 0)
194 buffer[MaxBufferExtent],
204 context=xmlNanoHTTPMethod(filename,(const char *) NULL,
205 (const char *) NULL,&type,(const char *) NULL,0);
206 if (context != (void *) NULL)
211 while ((bytes=xmlNanoHTTPRead(context,buffer,MaxBufferExtent)) > 0)
212 count=(ssize_t) fwrite(buffer,bytes,1,file);
213 xmlNanoHTTPClose(context);
215 xmlNanoHTTPCleanup();
220 *read_info->magick='\0';
221 image=ReadImage(read_info,exception);
222 if (unique_file != -1)
223 (void) RelinquishUniqueFileResource(read_info->filename);
224 read_info=DestroyImageInfo(read_info);
225 if (image == (Image *) NULL)
226 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
227 "NoDataReturned","`%s'",filename);
228 return(GetFirstImageInList(image));
232 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
236 % R e g i s t e r U R L I m a g e %
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
242 % RegisterURLImage() adds attributes for the URL image format to
243 % the list of supported formats. The attributes include the image format
244 % tag, a method to read and/or write the format, whether the format
245 % supports the saving of more than one frame to the same file or blob,
246 % whether the format supports native in-memory I/O, and a brief
247 % description of the format.
249 % The format of the RegisterURLImage method is:
251 % unsigned long RegisterURLImage(void)
254 ModuleExport unsigned long RegisterURLImage(void)
259 entry=SetMagickInfo("HTTP");
260 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
261 entry->decoder=(DecodeImageHandler *) ReadURLImage;
263 entry->description=ConstantString("Uniform Resource Locator (http://)");
264 entry->module=ConstantString("URL");
265 entry->stealth=MagickTrue;
266 (void) RegisterMagickInfo(entry);
267 entry=SetMagickInfo("FTP");
268 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
269 entry->decoder=(DecodeImageHandler *) ReadURLImage;
271 entry->description=ConstantString("Uniform Resource Locator (ftp://)");
272 entry->module=ConstantString("URL");
273 entry->stealth=MagickTrue;
274 (void) RegisterMagickInfo(entry);
275 entry=SetMagickInfo("FILE");
276 entry->decoder=(DecodeImageHandler *) ReadURLImage;
277 entry->description=ConstantString("Uniform Resource Locator (file://)");
278 entry->module=ConstantString("URL");
279 entry->stealth=MagickTrue;
280 (void) RegisterMagickInfo(entry);
281 return(MagickImageCoderSignature);
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
289 % U n r e g i s t e r U R L I m a g e %
293 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 % UnregisterURLImage() removes format registrations made by the
296 % URL module from the list of supported formats.
298 % The format of the UnregisterURLImage method is:
300 % UnregisterURLImage(void)
303 ModuleExport void UnregisterURLImage(void)
305 (void) UnregisterMagickInfo("HTTP");
306 (void) UnregisterMagickInfo("FTP");
307 (void) UnregisterMagickInfo("FILE");