2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Retrieve An Image Via URL. %
21 % Copyright 1999-2013 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 "MagickCore/studio.h"
44 #include "MagickCore/blob.h"
45 #include "MagickCore/blob-private.h"
46 #include "MagickCore/constitute.h"
47 #include "MagickCore/exception.h"
48 #include "MagickCore/exception-private.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/list.h"
52 #include "MagickCore/magick.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/module.h"
55 #include "MagickCore/quantum-private.h"
56 #include "MagickCore/static.h"
57 #include "MagickCore/resource_.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/utility.h"
60 #if defined(MAGICKCORE_XML_DELEGATE)
61 # if defined(MAGICKCORE_WINDOWS_SUPPORT)
62 # if defined(__MINGW32__) || defined(__MINGW64__)
65 # include <win32config.h>
68 # include <libxml/parser.h>
69 # include <libxml/xmlmemory.h>
70 # include <libxml/nanoftp.h>
71 # include <libxml/nanohttp.h>
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 % R e a d U R L I m a g e %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 % ReadURLImage retrieves an image via URL, decodes the image, and returns
86 % it. It allocates the memory necessary for the new Image structure and
87 % returns a pointer to the new image.
89 % The format of the ReadURLImage method is:
91 % Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
93 % A description of each parameter follows:
95 % o image_info: the image info.
97 % o exception: return any errors or warnings in this structure.
101 #if defined(__cplusplus) || defined(c_plusplus)
105 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
106 static void GetFTPData(void *userdata,const char *data,int size)
114 file=(FILE *) userdata;
115 if (file == (FILE *) NULL)
119 length=fwrite(data,size,1,file);
124 #if defined(__cplusplus) || defined(c_plusplus)
128 static Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
130 #define MaxBufferExtent 8192
133 filename[MaxTextExtent];
147 image=(Image *) NULL;
148 read_info=CloneImageInfo(image_info);
149 SetImageInfoBlob(read_info,(void *) NULL,0);
151 unique_file=AcquireUniqueFileResource(read_info->filename);
152 if (unique_file != -1)
153 file=fdopen(unique_file,"wb");
154 if ((unique_file == -1) || (file == (FILE *) NULL))
156 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
157 read_info->filename);
158 read_info=DestroyImageInfo(read_info);
159 return((Image *) NULL);
161 (void) CopyMagickString(filename,image_info->magick,MaxTextExtent);
162 (void) ConcatenateMagickString(filename,":",MaxTextExtent);
163 LocaleLower(filename);
164 (void) ConcatenateMagickString(filename,image_info->filename,MaxTextExtent);
165 if (LocaleCompare(read_info->magick,"file") == 0)
167 (void) RelinquishUniqueFileResource(read_info->filename);
169 (void) CopyMagickString(read_info->filename,image_info->filename+2,
172 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
173 if (LocaleCompare(read_info->magick,"ftp") == 0)
179 context=xmlNanoFTPNewCtxt(filename);
180 if (context != (void *) NULL)
182 if (xmlNanoFTPConnect(context) >= 0)
183 (void) xmlNanoFTPGet(context,GetFTPData,(void *) file,
185 (void) xmlNanoFTPClose(context);
189 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
190 if (LocaleCompare(read_info->magick,"http") == 0)
193 buffer[MaxBufferExtent],
203 context=xmlNanoHTTPMethod(filename,(const char *) NULL,
204 (const char *) NULL,&type,(const char *) NULL,0);
205 if (context != (void *) NULL)
210 while ((bytes=xmlNanoHTTPRead(context,buffer,MaxBufferExtent)) > 0)
211 count=(ssize_t) fwrite(buffer,bytes,1,file);
213 xmlNanoHTTPClose(context);
215 xmlNanoHTTPCleanup();
228 Guess image format from URL.
230 clone_info=CloneImageInfo(image_info);
231 sans=AcquireExceptionInfo();
232 (void) SetImageInfo(clone_info,0,sans);
233 (void) CopyMagickString(read_info->magick,clone_info->magick,MaxTextExtent);
234 clone_info=DestroyImageInfo(clone_info);
235 sans=DestroyExceptionInfo(sans);
237 image=ReadImage(read_info,exception);
238 if (unique_file != -1)
239 (void) RelinquishUniqueFileResource(read_info->filename);
240 read_info=DestroyImageInfo(read_info);
241 if (image != (Image *) NULL)
242 GetPathComponent(image_info->filename,TailPath,image->filename);
245 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
246 "NoDataReturned","`%s'",filename);
247 return((Image *) NULL);
249 return(GetFirstImageInList(image));
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
257 % R e g i s t e r U R L I m a g e %
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
263 % RegisterURLImage() adds attributes for the URL image format to
264 % the list of supported formats. The attributes include the image format
265 % tag, a method to read and/or write the format, whether the format
266 % supports the saving of more than one frame to the same file or blob,
267 % whether the format supports native in-memory I/O, and a brief
268 % description of the format.
270 % The format of the RegisterURLImage method is:
272 % size_t RegisterURLImage(void)
275 ModuleExport size_t RegisterURLImage(void)
280 entry=SetMagickInfo("HTTP");
281 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
282 entry->decoder=(DecodeImageHandler *) ReadURLImage;
284 entry->description=ConstantString("Uniform Resource Locator (http://)");
285 entry->module=ConstantString("URL");
286 entry->stealth=MagickTrue;
287 (void) RegisterMagickInfo(entry);
288 entry=SetMagickInfo("FTP");
289 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
290 entry->decoder=(DecodeImageHandler *) ReadURLImage;
292 entry->description=ConstantString("Uniform Resource Locator (ftp://)");
293 entry->module=ConstantString("URL");
294 entry->stealth=MagickTrue;
295 (void) RegisterMagickInfo(entry);
296 entry=SetMagickInfo("FILE");
297 entry->decoder=(DecodeImageHandler *) ReadURLImage;
298 entry->description=ConstantString("Uniform Resource Locator (file://)");
299 entry->module=ConstantString("URL");
300 entry->stealth=MagickTrue;
301 (void) RegisterMagickInfo(entry);
302 return(MagickImageCoderSignature);
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 % U n r e g i s t e r U R L I m a g e %
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 % UnregisterURLImage() removes format registrations made by the
317 % URL module from the list of supported formats.
319 % The format of the UnregisterURLImage method is:
321 % UnregisterURLImage(void)
324 ModuleExport void UnregisterURLImage(void)
326 (void) UnregisterMagickInfo("HTTP");
327 (void) UnregisterMagickInfo("FTP");
328 (void) UnregisterMagickInfo("FILE");