2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Retrieve An Image Via URL. %
21 % Copyright 1999-2017 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__)
63 # include <win32config.h>
66 # include <libxml/parser.h>
67 # include <libxml/xmlmemory.h>
68 # include <libxml/nanoftp.h>
69 # include <libxml/nanohttp.h>
71 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
72 !(defined(__MINGW32__) || defined(__MINGW64__))
74 # pragma comment(lib, "urlmon.lib")
78 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
82 % R e a d U R L I m a g e %
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88 % ReadURLImage retrieves an image via URL, decodes the image, and returns
89 % it. It allocates the memory necessary for the new Image structure and
90 % returns a pointer to the new image.
92 % The format of the ReadURLImage method is:
94 % Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
96 % A description of each parameter follows:
98 % o image_info: the image info.
100 % o exception: return any errors or warnings in this structure.
104 #if defined(__cplusplus) || defined(c_plusplus)
108 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
109 static void GetFTPData(void *userdata,const char *data,int size)
117 file=(FILE *) userdata;
118 if (file == (FILE *) NULL)
122 length=fwrite(data,size,1,file);
127 #if defined(__cplusplus) || defined(c_plusplus)
131 static Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
133 #define MaxBufferExtent 8192
136 filename[MagickPathExtent];
150 read_info=CloneImageInfo(image_info);
151 SetImageInfoBlob(read_info,(void *) NULL,0);
152 if (LocaleCompare(read_info->magick,"file") == 0)
154 (void) CopyMagickString(read_info->filename,image_info->filename+2,
156 *read_info->magick='\0';
157 image=ReadImage(read_info,exception);
158 read_info=DestroyImageInfo(read_info);
159 return(GetFirstImageInList(image));
162 unique_file=AcquireUniqueFileResource(read_info->filename);
163 if (unique_file != -1)
164 file=fdopen(unique_file,"wb");
165 if ((unique_file == -1) || (file == (FILE *) NULL))
167 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
168 read_info->filename);
169 read_info=DestroyImageInfo(read_info);
170 return((Image *) NULL);
172 (void) CopyMagickString(filename,image_info->magick,MagickPathExtent);
173 (void) ConcatenateMagickString(filename,":",MagickPathExtent);
174 LocaleLower(filename);
175 (void) ConcatenateMagickString(filename,image_info->filename,
177 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
178 !(defined(__MINGW32__) || defined(__MINGW64__))
180 if (URLDownloadToFile(NULL,filename,read_info->filename,0,NULL) != S_OK)
182 ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
184 (void) RelinquishUniqueFileResource(read_info->filename);
185 read_info=DestroyImageInfo(read_info);
186 return((Image *) NULL);
189 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
190 if (LocaleCompare(read_info->magick,"ftp") == 0)
196 context=xmlNanoFTPNewCtxt(filename);
197 if (context != (void *) NULL)
199 if (xmlNanoFTPConnect(context) >= 0)
200 (void) xmlNanoFTPGet(context,GetFTPData,(void *) file,
202 (void) xmlNanoFTPClose(context);
206 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
207 if (LocaleCompare(read_info->magick,"http") == 0)
210 buffer[MaxBufferExtent],
220 context=xmlNanoHTTPMethod(filename,(const char *) NULL,
221 (const char *) NULL,&type,(const char *) NULL,0);
222 if (context != (void *) NULL)
227 while ((bytes=xmlNanoHTTPRead(context,buffer,MaxBufferExtent)) > 0)
228 count=(ssize_t) fwrite(buffer,bytes,1,file);
230 xmlNanoHTTPClose(context);
232 xmlNanoHTTPCleanup();
238 *read_info->magick='\0';
239 image=ReadImage(read_info,exception);
240 (void) RelinquishUniqueFileResource(read_info->filename);
241 read_info=DestroyImageInfo(read_info);
242 if (image != (Image *) NULL)
243 GetPathComponent(image_info->filename,TailPath,image->filename);
246 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
247 "NoDataReturned","`%s'",filename);
248 return((Image *) NULL);
250 return(GetFirstImageInList(image));
254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
258 % R e g i s t e r U R L I m a g e %
262 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
264 % RegisterURLImage() adds attributes for the URL image format to
265 % the list of supported formats. The attributes include the image format
266 % tag, a method to read and/or write the format, whether the format
267 % supports the saving of more than one frame to the same file or blob,
268 % whether the format supports native in-memory I/O, and a brief
269 % description of the format.
271 % The format of the RegisterURLImage method is:
273 % size_t RegisterURLImage(void)
276 ModuleExport size_t RegisterURLImage(void)
281 entry=AcquireMagickInfo("URL","HTTP","Uniform Resource Locator (http://)");
282 #if (defined(MAGICKCORE_WINDOWS_SUPPORT) && \
283 !(defined(__MINGW32__) || defined(__MINGW64__))) || \
284 (defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED))
285 entry->decoder=(DecodeImageHandler *) ReadURLImage;
287 entry->format_type=ImplicitFormatType;
288 (void) RegisterMagickInfo(entry);
289 entry=AcquireMagickInfo("URL","HTTPS","Uniform Resource Locator (https://)");
290 #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
291 !(defined(__MINGW32__) || defined(__MINGW64__))
292 entry->decoder=(DecodeImageHandler *) ReadURLImage;
294 entry->format_type=ImplicitFormatType;
295 (void) RegisterMagickInfo(entry);
296 entry=AcquireMagickInfo("URL","FTP","Uniform Resource Locator (ftp://)");
297 #if (defined(MAGICKCORE_WINDOWS_SUPPORT) && \
298 !(defined(__MINGW32__) || defined(__MINGW64__))) || \
299 (defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED))
300 entry->decoder=(DecodeImageHandler *) ReadURLImage;
302 entry->format_type=ImplicitFormatType;
303 (void) RegisterMagickInfo(entry);
304 entry=AcquireMagickInfo("URL","FILE","Uniform Resource Locator (file://)");
305 entry->decoder=(DecodeImageHandler *) ReadURLImage;
306 entry->format_type=ImplicitFormatType;
307 (void) RegisterMagickInfo(entry);
308 return(MagickImageCoderSignature);
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 % U n r e g i s t e r U R L I m a g e %
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % UnregisterURLImage() removes format registrations made by the
323 % URL module from the list of supported formats.
325 % The format of the UnregisterURLImage method is:
327 % UnregisterURLImage(void)
330 ModuleExport void UnregisterURLImage(void)
332 (void) UnregisterMagickInfo("HTTP");
333 (void) UnregisterMagickInfo("FTP");
334 (void) UnregisterMagickInfo("FILE");