cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 % |
cristy | de984cd | 2013-12-01 14:49:27 +0000 | [diff] [blame] | 16 | % Cristy % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 17 | % Bill Radcliffe % |
| 18 | % March 2000 % |
| 19 | % % |
| 20 | % % |
cristy | b56bb24 | 2014-11-25 17:12:48 +0000 | [diff] [blame] | 21 | % Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization % |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 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 | |
| 40 | /* |
| 41 | Include declarations. |
| 42 | */ |
cristy | 4c08aed | 2011-07-01 19:47:50 +0000 | [diff] [blame] | 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" |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 60 | #if defined(MAGICKCORE_XML_DELEGATE) |
cristy | 0157aea | 2010-04-24 21:12:18 +0000 | [diff] [blame] | 61 | # if defined(MAGICKCORE_WINDOWS_SUPPORT) |
cristy | 14e6cc7 | 2014-10-05 19:31:31 +0000 | [diff] [blame] | 62 | # if !defined(__MINGW32__) && !defined(__MINGW64__) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 63 | # include <win32config.h> |
| 64 | # endif |
| 65 | # endif |
| 66 | # include <libxml/parser.h> |
| 67 | # include <libxml/xmlmemory.h> |
| 68 | # include <libxml/nanoftp.h> |
| 69 | # include <libxml/nanohttp.h> |
| 70 | #endif |
cristy | b0af14f | 2013-09-26 12:22:34 +0000 | [diff] [blame] | 71 | #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \ |
| 72 | !(defined(__MINGW32__) || defined(__MINGW64__)) |
| 73 | # include <urlmon.h> |
dirk | 5cb71bb | 2015-01-03 17:38:05 +0000 | [diff] [blame] | 74 | # pragma comment(lib, "urlmon.lib") |
dirk | 853b77b | 2013-09-21 19:04:42 +0000 | [diff] [blame] | 75 | #endif |
| 76 | |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 77 | /* |
| 78 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 79 | % % |
| 80 | % % |
| 81 | % % |
| 82 | % R e a d U R L I m a g e % |
| 83 | % % |
| 84 | % % |
| 85 | % % |
| 86 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 87 | % |
| 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. |
| 91 | % |
| 92 | % The format of the ReadURLImage method is: |
| 93 | % |
| 94 | % Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 95 | % |
| 96 | % A description of each parameter follows: |
| 97 | % |
| 98 | % o image_info: the image info. |
| 99 | % |
| 100 | % o exception: return any errors or warnings in this structure. |
| 101 | % |
| 102 | */ |
| 103 | |
| 104 | #if defined(__cplusplus) || defined(c_plusplus) |
| 105 | extern "C" { |
| 106 | #endif |
| 107 | |
| 108 | #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED) |
| 109 | static void GetFTPData(void *userdata,const char *data,int size) |
| 110 | { |
| 111 | FILE |
| 112 | *file; |
| 113 | |
| 114 | size_t |
| 115 | length; |
| 116 | |
| 117 | file=(FILE *) userdata; |
| 118 | if (file == (FILE *) NULL) |
| 119 | return; |
| 120 | if (size <= 0) |
| 121 | return; |
| 122 | length=fwrite(data,size,1,file); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 123 | (void) length; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 124 | } |
| 125 | #endif |
| 126 | |
| 127 | #if defined(__cplusplus) || defined(c_plusplus) |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | static Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception) |
| 132 | { |
| 133 | #define MaxBufferExtent 8192 |
| 134 | |
| 135 | char |
| 136 | filename[MaxTextExtent]; |
| 137 | |
| 138 | FILE |
| 139 | *file; |
| 140 | |
| 141 | Image |
| 142 | *image; |
| 143 | |
| 144 | ImageInfo |
| 145 | *read_info; |
| 146 | |
| 147 | int |
| 148 | unique_file; |
| 149 | |
| 150 | image=(Image *) NULL; |
| 151 | read_info=CloneImageInfo(image_info); |
| 152 | SetImageInfoBlob(read_info,(void *) NULL,0); |
| 153 | file=(FILE *) NULL; |
| 154 | unique_file=AcquireUniqueFileResource(read_info->filename); |
| 155 | if (unique_file != -1) |
| 156 | file=fdopen(unique_file,"wb"); |
| 157 | if ((unique_file == -1) || (file == (FILE *) NULL)) |
| 158 | { |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 159 | ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile", |
cristy | 12e6547 | 2013-04-05 11:49:29 +0000 | [diff] [blame] | 160 | read_info->filename); |
| 161 | read_info=DestroyImageInfo(read_info); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 162 | return((Image *) NULL); |
| 163 | } |
| 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) |
| 169 | { |
| 170 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 171 | unique_file=(-1); |
| 172 | (void) CopyMagickString(read_info->filename,image_info->filename+2, |
| 173 | MaxTextExtent); |
| 174 | } |
cristy | b0af14f | 2013-09-26 12:22:34 +0000 | [diff] [blame] | 175 | #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \ |
| 176 | !(defined(__MINGW32__) || defined(__MINGW64__)) |
| 177 | (void) fclose(file); |
dirk | 93b02b7 | 2013-11-16 16:03:36 +0000 | [diff] [blame] | 178 | if (URLDownloadToFile(NULL,filename,read_info->filename,0,NULL) != S_OK) |
cristy | b0af14f | 2013-09-26 12:22:34 +0000 | [diff] [blame] | 179 | { |
| 180 | ThrowFileException(exception,FileOpenError,"UnableToOpenFile", |
| 181 | filename); |
| 182 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 183 | read_info=DestroyImageInfo(read_info); |
| 184 | return((Image *) NULL); |
| 185 | } |
dirk | 853b77b | 2013-09-21 19:04:42 +0000 | [diff] [blame] | 186 | #else |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 187 | #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED) |
| 188 | if (LocaleCompare(read_info->magick,"ftp") == 0) |
| 189 | { |
| 190 | void |
| 191 | *context; |
| 192 | |
| 193 | xmlNanoFTPInit(); |
| 194 | context=xmlNanoFTPNewCtxt(filename); |
| 195 | if (context != (void *) NULL) |
| 196 | { |
| 197 | if (xmlNanoFTPConnect(context) >= 0) |
| 198 | (void) xmlNanoFTPGet(context,GetFTPData,(void *) file, |
| 199 | (char *) NULL); |
| 200 | (void) xmlNanoFTPClose(context); |
| 201 | } |
| 202 | } |
| 203 | #endif |
| 204 | #if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED) |
| 205 | if (LocaleCompare(read_info->magick,"http") == 0) |
| 206 | { |
| 207 | char |
| 208 | buffer[MaxBufferExtent], |
| 209 | *type; |
| 210 | |
| 211 | int |
| 212 | bytes; |
| 213 | |
| 214 | void |
| 215 | *context; |
| 216 | |
| 217 | type=(char *) NULL; |
| 218 | context=xmlNanoHTTPMethod(filename,(const char *) NULL, |
| 219 | (const char *) NULL,&type,(const char *) NULL,0); |
| 220 | if (context != (void *) NULL) |
| 221 | { |
| 222 | ssize_t |
| 223 | count; |
| 224 | |
| 225 | while ((bytes=xmlNanoHTTPRead(context,buffer,MaxBufferExtent)) > 0) |
| 226 | count=(ssize_t) fwrite(buffer,bytes,1,file); |
cristy | da16f16 | 2011-02-19 23:52:17 +0000 | [diff] [blame] | 227 | (void) count; |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 228 | xmlNanoHTTPClose(context); |
| 229 | xmlFree(type); |
| 230 | xmlNanoHTTPCleanup(); |
| 231 | } |
| 232 | } |
| 233 | #endif |
| 234 | (void) fclose(file); |
dirk | 853b77b | 2013-09-21 19:04:42 +0000 | [diff] [blame] | 235 | #endif |
cristy | 6ec529f | 2012-08-28 11:21:44 +0000 | [diff] [blame] | 236 | { |
| 237 | ExceptionInfo |
| 238 | *sans; |
| 239 | |
| 240 | ImageInfo |
| 241 | *clone_info; |
| 242 | |
| 243 | /* |
| 244 | Guess image format from URL. |
| 245 | */ |
| 246 | clone_info=CloneImageInfo(image_info); |
| 247 | sans=AcquireExceptionInfo(); |
| 248 | (void) SetImageInfo(clone_info,0,sans); |
| 249 | (void) CopyMagickString(read_info->magick,clone_info->magick,MaxTextExtent); |
| 250 | clone_info=DestroyImageInfo(clone_info); |
| 251 | sans=DestroyExceptionInfo(sans); |
| 252 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 253 | image=ReadImage(read_info,exception); |
| 254 | if (unique_file != -1) |
| 255 | (void) RelinquishUniqueFileResource(read_info->filename); |
| 256 | read_info=DestroyImageInfo(read_info); |
cristy | a134d33 | 2010-12-12 20:33:53 +0000 | [diff] [blame] | 257 | if (image != (Image *) NULL) |
| 258 | GetPathComponent(image_info->filename,TailPath,image->filename); |
| 259 | else |
cristy | 7ff4c44 | 2012-04-03 18:02:50 +0000 | [diff] [blame] | 260 | { |
| 261 | (void) ThrowMagickException(exception,GetMagickModule(),CoderError, |
| 262 | "NoDataReturned","`%s'",filename); |
| 263 | return((Image *) NULL); |
| 264 | } |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 265 | return(GetFirstImageInList(image)); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 270 | % % |
| 271 | % % |
| 272 | % % |
| 273 | % R e g i s t e r U R L I m a g e % |
| 274 | % % |
| 275 | % % |
| 276 | % % |
| 277 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 278 | % |
| 279 | % RegisterURLImage() adds attributes for the URL image format to |
| 280 | % the list of supported formats. The attributes include the image format |
| 281 | % tag, a method to read and/or write the format, whether the format |
| 282 | % supports the saving of more than one frame to the same file or blob, |
| 283 | % whether the format supports native in-memory I/O, and a brief |
| 284 | % description of the format. |
| 285 | % |
| 286 | % The format of the RegisterURLImage method is: |
| 287 | % |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 288 | % size_t RegisterURLImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 289 | % |
| 290 | */ |
cristy | bb50337 | 2010-05-27 20:51:26 +0000 | [diff] [blame] | 291 | ModuleExport size_t RegisterURLImage(void) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 292 | { |
| 293 | MagickInfo |
| 294 | *entry; |
| 295 | |
| 296 | entry=SetMagickInfo("HTTP"); |
cristy | b0af14f | 2013-09-26 12:22:34 +0000 | [diff] [blame] | 297 | #if (defined(MAGICKCORE_WINDOWS_SUPPORT) && \ |
| 298 | !(defined(__MINGW32__) || defined(__MINGW64__))) || \ |
dirk | 853b77b | 2013-09-21 19:04:42 +0000 | [diff] [blame] | 299 | (defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 300 | entry->decoder=(DecodeImageHandler *) ReadURLImage; |
| 301 | #endif |
| 302 | entry->description=ConstantString("Uniform Resource Locator (http://)"); |
| 303 | entry->module=ConstantString("URL"); |
| 304 | entry->stealth=MagickTrue; |
| 305 | (void) RegisterMagickInfo(entry); |
cristy | b0af14f | 2013-09-26 12:22:34 +0000 | [diff] [blame] | 306 | entry=SetMagickInfo("HTTPS"); |
| 307 | #if defined(MAGICKCORE_WINDOWS_SUPPORT) && \ |
| 308 | !(defined(__MINGW32__) || defined(__MINGW64__)) |
| 309 | entry->decoder=(DecodeImageHandler *) ReadURLImage; |
| 310 | #endif |
| 311 | entry->description=ConstantString("Uniform Resource Locator (https://)"); |
| 312 | entry->module=ConstantString("URL"); |
| 313 | entry->stealth=MagickTrue; |
dirk | 853b77b | 2013-09-21 19:04:42 +0000 | [diff] [blame] | 314 | (void) RegisterMagickInfo(entry); |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 315 | entry=SetMagickInfo("FTP"); |
cristy | b0af14f | 2013-09-26 12:22:34 +0000 | [diff] [blame] | 316 | #if (defined(MAGICKCORE_WINDOWS_SUPPORT) && \ |
| 317 | !(defined(__MINGW32__) || defined(__MINGW64__))) || \ |
dirk | 853b77b | 2013-09-21 19:04:42 +0000 | [diff] [blame] | 318 | (defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)) |
cristy | 3ed852e | 2009-09-05 21:47:34 +0000 | [diff] [blame] | 319 | entry->decoder=(DecodeImageHandler *) ReadURLImage; |
| 320 | #endif |
| 321 | entry->description=ConstantString("Uniform Resource Locator (ftp://)"); |
| 322 | entry->module=ConstantString("URL"); |
| 323 | entry->stealth=MagickTrue; |
| 324 | (void) RegisterMagickInfo(entry); |
| 325 | entry=SetMagickInfo("FILE"); |
| 326 | entry->decoder=(DecodeImageHandler *) ReadURLImage; |
| 327 | entry->description=ConstantString("Uniform Resource Locator (file://)"); |
| 328 | entry->module=ConstantString("URL"); |
| 329 | entry->stealth=MagickTrue; |
| 330 | (void) RegisterMagickInfo(entry); |
| 331 | return(MagickImageCoderSignature); |
| 332 | } |
| 333 | |
| 334 | /* |
| 335 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 336 | % % |
| 337 | % % |
| 338 | % % |
| 339 | % U n r e g i s t e r U R L I m a g e % |
| 340 | % % |
| 341 | % % |
| 342 | % % |
| 343 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 344 | % |
| 345 | % UnregisterURLImage() removes format registrations made by the |
| 346 | % URL module from the list of supported formats. |
| 347 | % |
| 348 | % The format of the UnregisterURLImage method is: |
| 349 | % |
| 350 | % UnregisterURLImage(void) |
| 351 | % |
| 352 | */ |
| 353 | ModuleExport void UnregisterURLImage(void) |
| 354 | { |
| 355 | (void) UnregisterMagickInfo("HTTP"); |
| 356 | (void) UnregisterMagickInfo("FTP"); |
| 357 | (void) UnregisterMagickInfo("FILE"); |
| 358 | } |