1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% U U RRRR L %
% U U R R L %
% U U RRRR L %
% U U R R L %
% UUU R R LLLLL %
% %
% %
% Retrieve An Image Via URL. %
% %
% Software Design %
% Cristy %
% Bill Radcliffe %
% March 2000 %
% %
% %
% Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
% dedicated to making software imaging solutions freely available. %
% %
% You may not use this file except in compliance with the License. You may %
% obtain a copy of the License at %
% %
% http://www.imagemagick.org/script/license.php %
% %
% Unless required by applicable law or agreed to in writing, software %
% distributed under the License is distributed on an "AS IS" BASIS, %
% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
% See the License for the specific language governing permissions and %
% limitations under the License. %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
*/
/*
Include declarations.
*/
#include "magick/studio.h"
#include "magick/blob.h"
#include "magick/blob-private.h"
#include "magick/constitute.h"
#include "magick/exception.h"
#include "magick/exception-private.h"
#include "magick/image.h"
#include "magick/image-private.h"
#include "magick/list.h"
#include "magick/magick.h"
#include "magick/memory_.h"
#include "magick/module.h"
#include "magick/pixel-accessor.h"
#include "magick/quantum-private.h"
#include "magick/static.h"
#include "magick/resource_.h"
#include "magick/string_.h"
#include "magick/utility.h"
#if defined(MAGICKCORE_XML_DELEGATE)
# if defined(MAGICKCORE_WINDOWS_SUPPORT)
# if !defined(__MINGW32__) && !defined(__MINGW64__)
# include <win32config.h>
# endif
# endif
# include <libxml/parser.h>
# include <libxml/xmlmemory.h>
# include <libxml/nanoftp.h>
# include <libxml/nanohttp.h>
#endif
#if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
!(defined(__MINGW32__) || defined(__MINGW64__))
# include <urlmon.h>
#endif
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e a d U R L I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% ReadURLImage retrieves an image via URL, decodes the image, and returns
% it. It allocates the memory necessary for the new Image structure and
% returns a pointer to the new image.
%
% The format of the ReadURLImage method is:
%
% Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
%
% A description of each parameter follows:
%
% o image_info: the image info.
%
% o exception: return any errors or warnings in this structure.
%
*/
#if defined(__cplusplus) || defined(c_plusplus)
extern "C" {
#endif
#if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
static void GetFTPData(void *userdata,const char *data,int size)
{
FILE
*file;
size_t
length;
file=(FILE *) userdata;
if (file == (FILE *) NULL)
return;
if (size <= 0)
return;
length=fwrite(data,size,1,file);
(void) length;
}
#endif
#if defined(__cplusplus) || defined(c_plusplus)
}
#endif
static Image *ReadURLImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
#define MaxBufferExtent 8192
char
filename[MaxTextExtent];
FILE
*file;
Image
*image;
ImageInfo
*read_info;
int
unique_file;
image=(Image *) NULL;
read_info=CloneImageInfo(image_info);
SetImageInfoBlob(read_info,(void *) NULL,0);
file=(FILE *) NULL;
unique_file=AcquireUniqueFileResource(read_info->filename);
if (unique_file != -1)
file=fdopen(unique_file,"wb");
if ((unique_file == -1) || (file == (FILE *) NULL))
{
ThrowFileException(exception,FileOpenError,"UnableToCreateTemporaryFile",
read_info->filename);
read_info=DestroyImageInfo(read_info);
return((Image *) NULL);
}
(void) CopyMagickString(filename,image_info->magick,MaxTextExtent);
(void) ConcatenateMagickString(filename,":",MaxTextExtent);
LocaleLower(filename);
(void) ConcatenateMagickString(filename,image_info->filename,MaxTextExtent);
if (LocaleCompare(read_info->magick,"file") == 0)
{
(void) RelinquishUniqueFileResource(read_info->filename);
unique_file=(-1);
(void) CopyMagickString(read_info->filename,image_info->filename+2,
MaxTextExtent);
}
#if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
!(defined(__MINGW32__) || defined(__MINGW64__))
(void) fclose(file);
if (URLDownloadToFile(NULL,filename,read_info->filename,0,NULL) != S_OK)
{
ThrowFileException(exception,FileOpenError,"UnableToOpenFile",
filename);
(void) RelinquishUniqueFileResource(read_info->filename);
read_info=DestroyImageInfo(read_info);
return((Image *) NULL);
}
#else
#if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED)
if (LocaleCompare(read_info->magick,"ftp") == 0)
{
void
*context;
xmlNanoFTPInit();
context=xmlNanoFTPNewCtxt(filename);
if (context != (void *) NULL)
{
if (xmlNanoFTPConnect(context) >= 0)
(void) xmlNanoFTPGet(context,GetFTPData,(void *) file,
(char *) NULL);
(void) xmlNanoFTPClose(context);
}
}
#endif
#if defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED)
if (LocaleCompare(read_info->magick,"http") == 0)
{
char
buffer[MaxBufferExtent],
*type;
int
bytes;
void
*context;
type=(char *) NULL;
context=xmlNanoHTTPMethod(filename,(const char *) NULL,
(const char *) NULL,&type,(const char *) NULL,0);
if (context != (void *) NULL)
{
ssize_t
count;
while ((bytes=xmlNanoHTTPRead(context,buffer,MaxBufferExtent)) > 0)
count=(ssize_t) fwrite(buffer,bytes,1,file);
(void) count;
xmlNanoHTTPClose(context);
xmlFree(type);
xmlNanoHTTPCleanup();
}
}
#endif
(void) fclose(file);
#endif
{
ExceptionInfo
*sans;
ImageInfo
*clone_info;
/*
Guess image format from URL.
*/
clone_info=CloneImageInfo(image_info);
sans=AcquireExceptionInfo();
(void) SetImageInfo(clone_info,0,sans);
(void) CopyMagickString(read_info->magick,clone_info->magick,MaxTextExtent);
clone_info=DestroyImageInfo(clone_info);
sans=DestroyExceptionInfo(sans);
}
image=ReadImage(read_info,exception);
if (unique_file != -1)
(void) RelinquishUniqueFileResource(read_info->filename);
read_info=DestroyImageInfo(read_info);
if (image != (Image *) NULL)
GetPathComponent(image_info->filename,TailPath,image->filename);
else
{
(void) ThrowMagickException(exception,GetMagickModule(),CoderError,
"NoDataReturned","`%s'",filename);
return((Image *) NULL);
}
return(GetFirstImageInList(image));
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% R e g i s t e r U R L I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% RegisterURLImage() adds attributes for the URL image format to
% the list of supported formats. The attributes include the image format
% tag, a method to read and/or write the format, whether the format
% supports the saving of more than one frame to the same file or blob,
% whether the format supports native in-memory I/O, and a brief
% description of the format.
%
% The format of the RegisterURLImage method is:
%
% size_t RegisterURLImage(void)
%
*/
ModuleExport size_t RegisterURLImage(void)
{
MagickInfo
*entry;
entry=SetMagickInfo("HTTP");
#if (defined(MAGICKCORE_WINDOWS_SUPPORT) && \
!(defined(__MINGW32__) || defined(__MINGW64__))) || \
(defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_HTTP_ENABLED))
entry->decoder=(DecodeImageHandler *) ReadURLImage;
#endif
entry->description=ConstantString("Uniform Resource Locator (http://)");
entry->module=ConstantString("URL");
entry->stealth=MagickTrue;
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("HTTPS");
#if defined(MAGICKCORE_WINDOWS_SUPPORT) && \
!(defined(__MINGW32__) || defined(__MINGW64__))
entry->decoder=(DecodeImageHandler *) ReadURLImage;
#endif
entry->description=ConstantString("Uniform Resource Locator (https://)");
entry->module=ConstantString("URL");
entry->stealth=MagickTrue;
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("FTP");
#if (defined(MAGICKCORE_WINDOWS_SUPPORT) && \
!(defined(__MINGW32__) || defined(__MINGW64__))) || \
(defined(MAGICKCORE_XML_DELEGATE) && defined(LIBXML_FTP_ENABLED))
entry->decoder=(DecodeImageHandler *) ReadURLImage;
#endif
entry->description=ConstantString("Uniform Resource Locator (ftp://)");
entry->module=ConstantString("URL");
entry->stealth=MagickTrue;
(void) RegisterMagickInfo(entry);
entry=SetMagickInfo("FILE");
entry->decoder=(DecodeImageHandler *) ReadURLImage;
entry->description=ConstantString("Uniform Resource Locator (file://)");
entry->module=ConstantString("URL");
entry->stealth=MagickTrue;
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);
}
/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% %
% %
% U n r e g i s t e r U R L I m a g e %
% %
% %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% UnregisterURLImage() removes format registrations made by the
% URL module from the list of supported formats.
%
% The format of the UnregisterURLImage method is:
%
% UnregisterURLImage(void)
%
*/
ModuleExport void UnregisterURLImage(void)
{
(void) UnregisterMagickInfo("HTTP");
(void) UnregisterMagickInfo("HTTPS");
(void) UnregisterMagickInfo("FTP");
(void) UnregisterMagickInfo("FILE");
}
|