1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-03-09 04:58:16 +03:00

parser: Introduce per-context resource loader

This commit is contained in:
Nick Wellnhofer 2024-06-11 03:51:43 +02:00
parent b9d2f3c911
commit 64ad272525
4 changed files with 67 additions and 8 deletions

12
SAX2.c
View File

@ -391,16 +391,14 @@ xmlSAX2ExternalSubset(void *ctx, const xmlChar *name,
* @publicId: The public ID of the entity
* @systemId: The system ID of the entity
*
* The entity loader, to control the loading of external entities,
* the application can either:
* - override this xmlSAX2ResolveEntity() callback in the SAX block
* - or better use the xmlSetExternalEntityLoader() function to
* set up it's own entity resolution routine
* This is only used to load DTDs. The preferred way to install
* custom resolvers is xmlCtxtSetResourceLoader.
*
* Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
* Returns a parser input.
*/
xmlParserInputPtr
xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId)
xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId,
const xmlChar *systemId)
{
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
xmlParserInputPtr ret = NULL;

View File

@ -159,6 +159,10 @@ typedef struct _xmlStartTag xmlStartTag;
typedef struct _xmlParserNsData xmlParserNsData;
typedef struct _xmlAttrHashBucket xmlAttrHashBucket;
typedef int
(*xmlResourceLoader)(void *ctxt, const char *url, const char *publicId,
int type, int flags, xmlParserInputPtr *out);
/**
* xmlParserCtxt:
*
@ -312,6 +316,9 @@ struct _xmlParserCtxt {
xmlStructuredErrorFunc errorHandler;
void *errorCtxt;
xmlResourceLoader resourceLoader;
void *resourceCtxt;
};
/**
@ -1197,6 +1204,10 @@ XMLPUBFUN void
xmlSetExternalEntityLoader(xmlExternalEntityLoader f);
XMLPUBFUN xmlExternalEntityLoader
xmlGetExternalEntityLoader(void);
XMLPUBFUN void
xmlCtxtSetResourceLoader(xmlParserCtxtPtr ctxt,
xmlResourceLoader loader,
void *vctxt);
XMLPUBFUN xmlParserInputPtr
xmlLoadExternalEntity (const char *URL,
const char *ID,

View File

@ -2306,6 +2306,8 @@ xmlDefaultExternalEntityLoader(const char *url, const char *ID,
* @ID: the System ID for the entity to load
* @ctxt: the context in which the entity is called or NULL
*
* DEPRECATED: Use XML_PARSE_NONET.
*
* A specific entity loader disabling network accesses, though still
* allowing local catalog accesses for resolution.
*
@ -2340,7 +2342,11 @@ xmlCurrentExternalEntityLoader = xmlDefaultExternalEntityLoader;
* xmlSetExternalEntityLoader:
* @f: the new entity resolver function
*
* Changes the defaultexternal entity resolver function for the application
* DEPRECATED: This is a global setting and not thread-safe. Use
* xmlCtxtSetResourceLoader or similar functions.
*
* Changes the default external entity resolver function for the
* application.
*/
void
xmlSetExternalEntityLoader(xmlExternalEntityLoader f) {
@ -2350,6 +2356,8 @@ xmlSetExternalEntityLoader(xmlExternalEntityLoader f) {
/**
* xmlGetExternalEntityLoader:
*
* DEPRECATED: See xmlSetExternalEntityLoader.
*
* Get the default external entity resolver function for the application
*
* Returns the xmlExternalEntityLoader function pointer
@ -2359,6 +2367,27 @@ xmlGetExternalEntityLoader(void) {
return(xmlCurrentExternalEntityLoader);
}
/**
* xmlCtxtSetResourceLoader:
* @ctxt: parser context
* @loader: callback
* @vctxt: user data
*
* Installs a custom callback to load documents, DTDs or external
* entities.
*
* Available since 2.14.0.
*/
void
xmlCtxtSetResourceLoader(xmlParserCtxtPtr ctxt, xmlResourceLoader loader,
void *vctxt) {
if (ctxt == NULL)
return;
ctxt->resourceLoader = loader;
ctxt->resourceCtxt = vctxt;
}
/**
* xmlLoadExternalEntity:
* @URL: the URL for the entity to load
@ -2375,6 +2404,7 @@ xmlGetExternalEntityLoader(void) {
* The following resource loaders will be called if they were
* registered (in order of precedence):
*
* - the resource loader set with xmlCtxtSetResourceLoader
* - the global external entity loader set with
* xmlSetExternalEntityLoader
* - the per-thread xmlParserInputBufferCreateFilenameFunc set with
@ -2397,6 +2427,22 @@ xmlLoadExternalEntity(const char *URL, const char *ID,
if (URL == NULL)
return(NULL);
if ((ctxt != NULL) && (ctxt->resourceLoader != NULL)) {
int flags = 0;
int code;
if ((ctxt->options & XML_PARSE_NO_UNZIP) == 0)
flags |= XML_INPUT_UNZIP;
code = ctxt->resourceLoader(ctxt->resourceCtxt, URL, ID, flags,
0, &ret);
if (code != XML_ERR_OK) {
xmlCtxtErrIO(ctxt, code, URL);
return(NULL);
}
return(ret);
}
canonicFilename = (char *) xmlCanonicPath((const xmlChar *) URL);
if (canonicFilename == NULL) {
xmlCtxtErrMemory(ctxt);

View File

@ -2129,6 +2129,8 @@ xmlOutputBufferCreateIO(xmlOutputWriteCallback iowrite,
* xmlParserInputBufferCreateFilenameDefault:
* @func: function pointer to the new ParserInputBufferCreateFilenameFunc
*
* DEPRECATED: Use xmlCtxtSetResourceLoader or similar functions.
*
* Registers a callback for URI input file handling
*
* Returns the old value of the registration function
@ -2888,6 +2890,8 @@ xmlInitIOCallbacks(void)
* @readFunc: the xmlInputReadCallback
* @closeFunc: the xmlInputCloseCallback
*
* DEPRECATED: Use xmlCtxtSetResourceLoader or similar functions.
*
* Register a new set of I/O callback for handling parser input.
*
* Returns the registered handler number or -1 in case of error