2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Retrieve An Image Via URL. %
21 % Copyright 1999-2014 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>
73 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
74 !(defined(__MINGW32__) || defined(__MINGW64__))
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % R e a d U R L I m a g e %
87 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89 % ReadURLImage retrieves an image via URL, decodes the image, and returns
90 % it. It allocates the memory necessary for the new Image structure and
91 % returns a pointer to the new image.
93 % The format of the ReadURLImage method is:
95 % Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
97 % A description of each parameter follows:
99 % o image_info: the image info.
101 % o exception: return any errors or warnings in this structure.
105 #if defined(__cplusplus) || defined(c_plusplus)
109 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
110 static void GetFTPData(void *userdata,const char *data,int size)
118 file=(FILE *) userdata;
119 if (file == (FILE *) NULL)
123 length=fwrite(data,size,1,file);
128 #if defined(__cplusplus) || defined(c_plusplus)
132 static Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
134 #define MaxBufferExtent 8192
137 filename[MaxTextExtent];
151 image=(Image *) NULL;
152 read_info=CloneImageInfo(image_info);
153 SetImageInfoBlob(read_info,(void *) NULL,0);
155 unique_file=AcquireUniqueFileResource(read_info->filename);
156 if (unique_file != -1)
157 file=fdopen(unique_file,"wb");
158 if ((unique_file == -1) || (file == (FILE *) NULL))
160 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
161 read_info->filename);
162 read_info=DestroyImageInfo(read_info);
163 return((Image *) NULL);
165 (void) CopyMagickString(filename,image_info->magick,MaxTextExtent);
166 (void) ConcatenateMagickString(filename,":",MaxTextExtent);
167 LocaleLower(filename);
168 (void) ConcatenateMagickString(filename,image_info->filename,MaxTextExtent);
169 if (LocaleCompare(read_info->magick,"file") == 0)
171 (void) RelinquishUniqueFileResource(read_info->filename);
173 (void) CopyMagickString(read_info->filename,image_info->filename+2,
176 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
177 !(defined(__MINGW32__) || defined(__MINGW64__))
179 if (URLDownloadToFile(NULL,filename,read_info->filename,0,NULL) != S_OK)
181 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
183 (void) RelinquishUniqueFileResource(read_info->filename);
184 read_info=DestroyImageInfo(read_info);
185 return((Image *) NULL);
188 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
189 if (LocaleCompare(read_info->magick,"ftp") == 0)
195 context=xmlNanoFTPNewCtxt(filename);
196 if (context != (void *) NULL)
198 if (xmlNanoFTPConnect(context) >= 0)
199 (void) xmlNanoFTPGet(context,GetFTPData,(void *) file,
201 (void) xmlNanoFTPClose(context);
205 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
206 if (LocaleCompare(read_info->magick,"http") == 0)
209 buffer[MaxBufferExtent],
219 context=xmlNanoHTTPMethod(filename,(const char *) NULL,
220 (const char *) NULL,&type,(const char *) NULL,0);
221 if (context != (void *) NULL)
226 while ((bytes=xmlNanoHTTPRead(context,buffer,MaxBufferExtent)) > 0)
227 count=(ssize_t) fwrite(buffer,bytes,1,file);
229 xmlNanoHTTPClose(context);
231 xmlNanoHTTPCleanup();
245 Guess image format from URL.
247 clone_info=CloneImageInfo(image_info);
248 sans=AcquireExceptionInfo();
249 (void) SetImageInfo(clone_info,0,sans);
250 (void) CopyMagickString(read_info->magick,clone_info->magick,MaxTextExtent);
251 clone_info=DestroyImageInfo(clone_info);
252 sans=DestroyExceptionInfo(sans);
254 image=ReadImage(read_info,exception);
255 if (unique_file != -1)
256 (void) RelinquishUniqueFileResource(read_info->filename);
257 read_info=DestroyImageInfo(read_info);
258 if (image != (Image *) NULL)
259 GetPathComponent(image_info->filename,TailPath,image->filename);
262 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
263 "NoDataReturned","`%s'",filename);
264 return((Image *) NULL);
266 return(GetFirstImageInList(image));
270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 % R e g i s t e r U R L I m a g e %
278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % RegisterURLImage() adds attributes for the URL image format to
281 % the list of supported formats. The attributes include the image format
282 % tag, a method to read and/or write the format, whether the format
283 % supports the saving of more than one frame to the same file or blob,
284 % whether the format supports native in-memory I/O, and a brief
285 % description of the format.
287 % The format of the RegisterURLImage method is:
289 % size_t RegisterURLImage(void)
292 ModuleExport size_t RegisterURLImage(void)
297 entry=SetMagickInfo("HTTP");
298 #if (defined(MAGICKCORE_WINDOWS_SUPPORT) && \
299 !(defined(__MINGW32__) || defined(__MINGW64__))) || \
300 (defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED))
301 entry->decoder=(DecodeImageHandler *) ReadURLImage;
303 entry->description=ConstantString("Uniform Resource Locator (http://)");
304 entry->module=ConstantString("URL");
305 entry->stealth=MagickTrue;
306 (void) RegisterMagickInfo(entry);
307 entry=SetMagickInfo("HTTPS");
308 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
309 !(defined(__MINGW32__) || defined(__MINGW64__))
310 entry->decoder=(DecodeImageHandler *) ReadURLImage;
312 entry->description=ConstantString("Uniform Resource Locator (https://)");
313 entry->module=ConstantString("URL");
314 entry->stealth=MagickTrue;
315 (void) RegisterMagickInfo(entry);
316 entry=SetMagickInfo("FTP");
317 #if (defined(MAGICKCORE_WINDOWS_SUPPORT) && \
318 !(defined(__MINGW32__) || defined(__MINGW64__))) || \
319 (defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED))
320 entry->decoder=(DecodeImageHandler *) ReadURLImage;
322 entry->description=ConstantString("Uniform Resource Locator (ftp://)");
323 entry->module=ConstantString("URL");
324 entry->stealth=MagickTrue;
325 (void) RegisterMagickInfo(entry);
326 entry=SetMagickInfo("FILE");
327 entry->decoder=(DecodeImageHandler *) ReadURLImage;
328 entry->description=ConstantString("Uniform Resource Locator (file://)");
329 entry->module=ConstantString("URL");
330 entry->stealth=MagickTrue;
331 (void) RegisterMagickInfo(entry);
332 return(MagickImageCoderSignature);
336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340 % U n r e g i s t e r U R L I m a g e %
344 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346 % UnregisterURLImage() removes format registrations made by the
347 % URL module from the list of supported formats.
349 % The format of the UnregisterURLImage method is:
351 % UnregisterURLImage(void)
354 ModuleExport void UnregisterURLImage(void)
356 (void) UnregisterMagickInfo("HTTP");
357 (void) UnregisterMagickInfo("FTP");
358 (void) UnregisterMagickInfo("FILE");