2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13 % Retrieve An Image Via URL. %
21 % Copyright 1999-2011 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__)
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 read_info=DestroyImageInfo(read_info);
157 (void) CopyMagickString(image->filename,read_info->filename,
159 ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
161 image=DestroyImageList(image);
162 return((Image *) NULL);
164 (void) CopyMagickString(filename,image_info->magick,MaxTextExtent);
165 (void) ConcatenateMagickString(filename,":",MaxTextExtent);
166 LocaleLower(filename);
167 (void) ConcatenateMagickString(filename,image_info->filename,MaxTextExtent);
168 if (LocaleCompare(read_info->magick,"file") == 0)
170 (void) RelinquishUniqueFileResource(read_info->filename);
172 (void) CopyMagickString(read_info->filename,image_info->filename+2,
175 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
176 if (LocaleCompare(read_info->magick,"ftp") == 0)
182 context=xmlNanoFTPNewCtxt(filename);
183 if (context != (void *) NULL)
185 if (xmlNanoFTPConnect(context) >= 0)
186 (void) xmlNanoFTPGet(context,GetFTPData,(void *) file,
188 (void) xmlNanoFTPClose(context);
192 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
193 if (LocaleCompare(read_info->magick,"http") == 0)
196 buffer[MaxBufferExtent],
206 context=xmlNanoHTTPMethod(filename,(const char *) NULL,
207 (const char *) NULL,&type,(const char *) NULL,0);
208 if (context != (void *) NULL)
213 while ((bytes=xmlNanoHTTPRead(context,buffer,MaxBufferExtent)) > 0)
214 count=(ssize_t) fwrite(buffer,bytes,1,file);
216 xmlNanoHTTPClose(context);
218 xmlNanoHTTPCleanup();
223 *read_info->magick='\0';
224 image=ReadImage(read_info,exception);
225 if (unique_file != -1)
226 (void) RelinquishUniqueFileResource(read_info->filename);
227 read_info=DestroyImageInfo(read_info);
228 if (image != (Image *) NULL)
229 GetPathComponent(image_info->filename,TailPath,image->filename);
231 (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
232 "NoDataReturned","`%s'",filename);
233 return(GetFirstImageInList(image));
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 % R e g i s t e r U R L I m a g e %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % RegisterURLImage() adds attributes for the URL image format to
248 % the list of supported formats. The attributes include the image format
249 % tag, a method to read and/or write the format, whether the format
250 % supports the saving of more than one frame to the same file or blob,
251 % whether the format supports native in-memory I/O, and a brief
252 % description of the format.
254 % The format of the RegisterURLImage method is:
256 % size_t RegisterURLImage(void)
259 ModuleExport size_t RegisterURLImage(void)
264 entry=SetMagickInfo("HTTP");
265 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
266 entry->decoder=(DecodeImageHandler *) ReadURLImage;
268 entry->description=ConstantString("Uniform Resource Locator (http://)");
269 entry->module=ConstantString("URL");
270 entry->stealth=MagickTrue;
271 (void) RegisterMagickInfo(entry);
272 entry=SetMagickInfo("FTP");
273 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
274 entry->decoder=(DecodeImageHandler *) ReadURLImage;
276 entry->description=ConstantString("Uniform Resource Locator (ftp://)");
277 entry->module=ConstantString("URL");
278 entry->stealth=MagickTrue;
279 (void) RegisterMagickInfo(entry);
280 entry=SetMagickInfo("FILE");
281 entry->decoder=(DecodeImageHandler *) ReadURLImage;
282 entry->description=ConstantString("Uniform Resource Locator (file://)");
283 entry->module=ConstantString("URL");
284 entry->stealth=MagickTrue;
285 (void) RegisterMagickInfo(entry);
286 return(MagickImageCoderSignature);
290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
294 % U n r e g i s t e r U R L I m a g e %
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 % UnregisterURLImage() removes format registrations made by the
301 % URL module from the list of supported formats.
303 % The format of the UnregisterURLImage method is:
305 % UnregisterURLImage(void)
308 ModuleExport void UnregisterURLImage(void)
310 (void) UnregisterMagickInfo("HTTP");
311 (void) UnregisterMagickInfo("FTP");
312 (void) UnregisterMagickInfo("FILE");