PostgreSQL Source Code  git master
xslt_proc.c File Reference
#include "postgres.h"
#include "executor/spi.h"
#include "fmgr.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "utils/builtins.h"
#include "utils/xml.h"
Include dependency graph for xslt_proc.c:

Go to the source code of this file.

Functions

 PG_FUNCTION_INFO_V1 (xslt_process)
 
Datum xslt_process (PG_FUNCTION_ARGS)
 

Function Documentation

◆ PG_FUNCTION_INFO_V1()

PG_FUNCTION_INFO_V1 ( xslt_process  )

◆ xslt_process()

Datum xslt_process ( PG_FUNCTION_ARGS  )

Definition at line 48 of file xslt_proc.c.

49 {
50 #ifdef USE_LIBXSLT
51 
52  text *doct = PG_GETARG_TEXT_PP(0);
53  text *ssheet = PG_GETARG_TEXT_PP(1);
54  text *result;
55  text *paramstr;
56  const char **params;
57  PgXmlErrorContext *xmlerrcxt;
58  volatile xsltStylesheetPtr stylesheet = NULL;
59  volatile xmlDocPtr doctree = NULL;
60  volatile xmlDocPtr restree = NULL;
61  volatile xsltSecurityPrefsPtr xslt_sec_prefs = NULL;
62  volatile xsltTransformContextPtr xslt_ctxt = NULL;
63  volatile int resstat = -1;
64  xmlChar *resstr = NULL;
65  int reslen = 0;
66 
67  if (fcinfo->nargs == 3)
68  {
69  paramstr = PG_GETARG_TEXT_PP(2);
70  params = parse_params(paramstr);
71  }
72  else
73  {
74  /* No parameters */
75  params = (const char **) palloc(sizeof(char *));
76  params[0] = NULL;
77  }
78 
79  /* Setup parser */
81 
82  PG_TRY();
83  {
84  xmlDocPtr ssdoc;
85  bool xslt_sec_prefs_error;
86 
87  /* Parse document */
88  doctree = xmlReadMemory((char *) VARDATA_ANY(doct),
89  VARSIZE_ANY_EXHDR(doct), NULL, NULL,
90  XML_PARSE_NOENT);
91 
92  if (doctree == NULL)
93  xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
94  "error parsing XML document");
95 
96  /* Same for stylesheet */
97  ssdoc = xmlReadMemory((char *) VARDATA_ANY(ssheet),
98  VARSIZE_ANY_EXHDR(ssheet), NULL, NULL,
99  XML_PARSE_NOENT);
100 
101  if (ssdoc == NULL)
102  xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
103  "error parsing stylesheet as XML document");
104 
105  /* After this call we need not free ssdoc separately */
106  stylesheet = xsltParseStylesheetDoc(ssdoc);
107 
108  if (stylesheet == NULL)
109  xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
110  "failed to parse stylesheet");
111 
112  xslt_ctxt = xsltNewTransformContext(stylesheet, doctree);
113 
114  xslt_sec_prefs_error = false;
115  if ((xslt_sec_prefs = xsltNewSecurityPrefs()) == NULL)
116  xslt_sec_prefs_error = true;
117 
118  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_FILE,
119  xsltSecurityForbid) != 0)
120  xslt_sec_prefs_error = true;
121  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_FILE,
122  xsltSecurityForbid) != 0)
123  xslt_sec_prefs_error = true;
124  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_CREATE_DIRECTORY,
125  xsltSecurityForbid) != 0)
126  xslt_sec_prefs_error = true;
127  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_READ_NETWORK,
128  xsltSecurityForbid) != 0)
129  xslt_sec_prefs_error = true;
130  if (xsltSetSecurityPrefs(xslt_sec_prefs, XSLT_SECPREF_WRITE_NETWORK,
131  xsltSecurityForbid) != 0)
132  xslt_sec_prefs_error = true;
133  if (xsltSetCtxtSecurityPrefs(xslt_sec_prefs, xslt_ctxt) != 0)
134  xslt_sec_prefs_error = true;
135 
136  if (xslt_sec_prefs_error)
137  ereport(ERROR,
138  (errmsg("could not set libxslt security preferences")));
139 
140  restree = xsltApplyStylesheetUser(stylesheet, doctree, params,
141  NULL, NULL, xslt_ctxt);
142 
143  if (restree == NULL)
144  xml_ereport(xmlerrcxt, ERROR, ERRCODE_EXTERNAL_ROUTINE_EXCEPTION,
145  "failed to apply stylesheet");
146 
147  resstat = xsltSaveResultToString(&resstr, &reslen, restree, stylesheet);
148  }
149  PG_CATCH();
150  {
151  if (restree != NULL)
152  xmlFreeDoc(restree);
153  if (xslt_ctxt != NULL)
154  xsltFreeTransformContext(xslt_ctxt);
155  if (xslt_sec_prefs != NULL)
156  xsltFreeSecurityPrefs(xslt_sec_prefs);
157  if (stylesheet != NULL)
158  xsltFreeStylesheet(stylesheet);
159  if (doctree != NULL)
160  xmlFreeDoc(doctree);
161  xsltCleanupGlobals();
162 
163  pg_xml_done(xmlerrcxt, true);
164 
165  PG_RE_THROW();
166  }
167  PG_END_TRY();
168 
169  xmlFreeDoc(restree);
170  xsltFreeTransformContext(xslt_ctxt);
171  xsltFreeSecurityPrefs(xslt_sec_prefs);
172  xsltFreeStylesheet(stylesheet);
173  xmlFreeDoc(doctree);
174  xsltCleanupGlobals();
175 
176  pg_xml_done(xmlerrcxt, false);
177 
178  /* XXX this is pretty dubious, really ought to throw error instead */
179  if (resstat < 0)
180  PG_RETURN_NULL();
181 
182  result = cstring_to_text_with_len((char *) resstr, reslen);
183 
184  if (resstr)
185  xmlFree(resstr);
186 
187  PG_RETURN_TEXT_P(result);
188 #else /* !USE_LIBXSLT */
189 
190  ereport(ERROR,
191  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
192  errmsg("xslt_process() is not available without libxslt")));
193  PG_RETURN_NULL();
194 #endif /* USE_LIBXSLT */
195 }
int errcode(int sqlerrcode)
Definition: elog.c:860
int errmsg(const char *fmt,...)
Definition: elog.c:1075
#define PG_RE_THROW()
Definition: elog.h:411
#define PG_TRY(...)
Definition: elog.h:370
#define PG_END_TRY(...)
Definition: elog.h:395
#define ERROR
Definition: elog.h:39
#define PG_CATCH(...)
Definition: elog.h:380
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_RETURN_TEXT_P(x)
Definition: fmgr.h:372
void * palloc(Size size)
Definition: mcxt.c:1201
Definition: c.h:676
#define VARDATA_ANY(PTR)
Definition: varatt.h:324
#define VARSIZE_ANY_EXHDR(PTR)
Definition: varatt.h:317
text * cstring_to_text_with_len(const char *s, int len)
Definition: varlena.c:196
struct PgXmlErrorContext PgXmlErrorContext
Definition: xml.h:48
void xml_ereport(PgXmlErrorContext *errcxt, int level, int sqlcode, const char *msg)
void pg_xml_done(PgXmlErrorContext *errcxt, bool isError)
@ PG_XML_STRICTNESS_LEGACY
Definition: xml.h:41
PgXmlErrorContext * pgxml_parser_init(PgXmlStrictness strictness)
Definition: xpath.c:65

References cstring_to_text_with_len(), ereport, errcode(), errmsg(), ERROR, palloc(), PG_CATCH, PG_END_TRY, PG_GETARG_TEXT_PP, PG_RE_THROW, PG_RETURN_NULL, PG_RETURN_TEXT_P, PG_TRY, pg_xml_done(), PG_XML_STRICTNESS_LEGACY, pgxml_parser_init(), VARDATA_ANY, VARSIZE_ANY_EXHDR, and xml_ereport().