]> granicus.if.org Git - imagemagick/blob - coders/url.c
(no commit message)
[imagemagick] / coders / url.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                            U   U  RRRR   L                                  %
7 %                            U   U  R   R  L                                  %
8 %                            U   U  RRRR   L                                  %
9 %                            U   U  R R    L                                  %
10 %                             UUU   R  R   LLLLL                              %
11 %                                                                             %
12 %                                                                             %
13 %                        Retrieve An Image Via URL.                           %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                              Bill Radcliffe                                 %
18 %                                March 2000                                   %
19 %                                                                             %
20 %                                                                             %
21 %  Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization      %
22 %  dedicated to making software imaging solutions freely available.           %
23 %                                                                             %
24 %  You may not use this file except in compliance with the License.  You may  %
25 %  obtain a copy of the License at                                            %
26 %                                                                             %
27 %    http://www.imagemagick.org/script/license.php                            %
28 %                                                                             %
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.                                             %
34 %                                                                             %
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 %
37 %
38 */
39 \f
40 /*
41   Include declarations.
42 */
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 #      define _MSC_VER
64 #    else
65 #      include <win32config.h>
66 #    endif
67 #  endif
68 #  include <libxml/parser.h>
69 #  include <libxml/xmlmemory.h>
70 #  include <libxml/nanoftp.h>
71 #  include <libxml/nanohttp.h>
72 #endif
73 \f
74 /*
75 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %   R e a d U R L I m a g e                                                   %
80 %                                                                             %
81 %                                                                             %
82 %                                                                             %
83 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 %
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.
88 %
89 %  The format of the ReadURLImage method is:
90 %
91 %      Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
92 %
93 %  A description of each parameter follows:
94 %
95 %    o image_info: the image info.
96 %
97 %    o exception: return any errors or warnings in this structure.
98 %
99 */
100
101 #if defined(__cplusplus) || defined(c_plusplus)
102 extern "C" {
103 #endif
104
105 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
106 static void GetFTPData(void *userdata,const char *data,int size)
107 {
108   FILE
109     *file;
110
111   size_t
112     length;
113
114   file=(FILE *) userdata;
115   if (file == (FILE *) NULL)
116     return;
117   if (size <= 0)
118     return;
119   length=fwrite(data,size,1,file);
120   (void) length;
121 }
122 #endif
123
124 #if defined(__cplusplus) || defined(c_plusplus)
125 }
126 #endif
127
128 static Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
129 {
130 #define MaxBufferExtent  8192
131
132   char
133     filename[MaxTextExtent];
134
135   FILE
136     *file;
137
138   Image
139     *image;
140
141   ImageInfo
142     *read_info;
143
144   int
145     unique_file;
146
147   image=(Image *) NULL;
148   read_info=CloneImageInfo(image_info);
149   SetImageInfoBlob(read_info,(void *) NULL,0);
150   file=(FILE *) NULL;
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))
155     {
156       ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
157         read_info->filename);
158       read_info=DestroyImageInfo(read_info);
159       return((Image *) NULL);
160     }
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)
166     {
167       (void) RelinquishUniqueFileResource(read_info->filename);
168       unique_file=(-1);
169       (void) CopyMagickString(read_info->filename,image_info->filename+2,
170         MaxTextExtent);
171     }
172 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
173   if (LocaleCompare(read_info->magick,"ftp") == 0)
174     {
175       void
176         *context;
177
178       xmlNanoFTPInit();
179       context=xmlNanoFTPNewCtxt(filename);
180       if (context != (void *) NULL)
181         {
182           if (xmlNanoFTPConnect(context) >= 0)
183             (void) xmlNanoFTPGet(context,GetFTPData,(void *) file,
184               (char *) NULL);
185           (void) xmlNanoFTPClose(context);
186         }
187     }
188 #endif
189 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
190   if (LocaleCompare(read_info->magick,"http") == 0)
191     {
192       char
193         buffer[MaxBufferExtent],
194         *type;
195
196       int
197         bytes;
198
199       void
200         *context;
201
202       type=(char *) NULL;
203       context=xmlNanoHTTPMethod(filename,(const char *) NULL,
204         (const char *) NULL,&type,(const char *) NULL,0);
205       if (context != (void *) NULL)
206         {
207           ssize_t
208             count;
209
210           while ((bytes=xmlNanoHTTPRead(context,buffer,MaxBufferExtent)) > 0)
211             count=(ssize_t) fwrite(buffer,bytes,1,file);
212           (void) count;
213           xmlNanoHTTPClose(context);
214           xmlFree(type);
215           xmlNanoHTTPCleanup();
216         }
217     }
218 #endif
219   (void) fclose(file);
220   {
221     ExceptionInfo
222       *sans;
223
224     ImageInfo
225       *clone_info;
226
227     /*
228       Guess image format from URL.
229     */
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);
236   }
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);
243   else
244     {
245       (void) ThrowMagickException(exception,GetMagickModule(),CoderError,
246         "NoDataReturned","`%s'",filename);
247       return((Image *) NULL);
248     }
249   return(GetFirstImageInList(image));
250 }
251 \f
252 /*
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 %                                                                             %
255 %                                                                             %
256 %                                                                             %
257 %   R e g i s t e r U R L I m a g e                                           %
258 %                                                                             %
259 %                                                                             %
260 %                                                                             %
261 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
262 %
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.
269 %
270 %  The format of the RegisterURLImage method is:
271 %
272 %      size_t RegisterURLImage(void)
273 %
274 */
275 ModuleExport size_t RegisterURLImage(void)
276 {
277   MagickInfo
278     *entry;
279
280   entry=SetMagickInfo("HTTP");
281 #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
282   entry->decoder=(DecodeImageHandler *) ReadURLImage;
283 #endif
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;
291 #endif
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);
303 }
304 \f
305 /*
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 %                                                                             %
308 %                                                                             %
309 %                                                                             %
310 %   U n r e g i s t e r U R L I m a g e                                       %
311 %                                                                             %
312 %                                                                             %
313 %                                                                             %
314 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 %
316 %  UnregisterURLImage() removes format registrations made by the
317 %  URL module from the list of supported formats.
318 %
319 %  The format of the UnregisterURLImage method is:
320 %
321 %      UnregisterURLImage(void)
322 %
323 */
324 ModuleExport void UnregisterURLImage(void)
325 {
326   (void) UnregisterMagickInfo("HTTP");
327   (void) UnregisterMagickInfo("FTP");
328   (void) UnregisterMagickInfo("FILE");
329 }