[PATCH] Return http status code from XSLT

SamB ja.nginx at mailnull.com
Thu Mar 7 23:06:48 UTC 2013


Hi,

    this patch provides simple possibility to return http error code 
from within XSLT transformation result.
    This is simple way to quickly and correctly return i.e. 404 error 
codes instead of producing dummy soft-404 pages.

Sample XSLT:
<xsl:template match="/">
<html xmlns:http="http://www.w3.org/http" status="400">
         <xsl:copy-of select="."/>
</html>
</xsl:template>


Thanks
    Sam


  src/http/modules/ngx_http_xslt_filter_module.c | 22 ++++++++++++++++++++++
  1 file changed, 22 insertions(+)

diff --git a/src/http/modules/ngx_http_xslt_filter_module.c 
b/src/http/modules/ngx_http_xslt_filter_module.c
index a6ae1ce..c5710d5 100644
--- a/src/http/modules/ngx_http_xslt_filter_module.c
+++ b/src/http/modules/ngx_http_xslt_filter_module.c
@@ -27,6 +27,9 @@
  #endif


+#define NGX_HTTP_XSLT_NS "http://www.w3.org/http"
+
+
  typedef struct {
      u_char                    *name;
      void                      *data;
@@ -477,6 +480,7 @@ ngx_http_xslt_apply_stylesheet(ngx_http_request_t *r,
      ngx_uint_t                        i;
      xmlChar                          *buf;
      xmlDocPtr                         doc, res;
+    xmlNodePtr                        node;
      ngx_http_xslt_sheet_t            *sheet;
      ngx_http_xslt_filter_loc_conf_t  *conf;

@@ -551,6 +555,24 @@ ngx_http_xslt_apply_stylesheet(ngx_http_request_t *r,
                     doc_type, type ? type : (u_char *) "(null)",
                     encoding ? encoding : (u_char *) "(null)");

+    node = xmlDocGetRootElement(doc);
+    if (node != NULL) {
+        xmlChar *attr = NULL;
+
+        attr = xmlGetNsProp(node, (const xmlChar *)"status", (const 
xmlChar *)NGX_HTTP_XSLT_NS);
+        if (attr != NULL
+            && attr[0] != '\0') {
+
+            ngx_uint_t status = strtoul((const char *)attr, NULL, 10);
+
+            if (status > 0
+               && r->headers_out.status != status) {
+                r->headers_out.status = status;
+                r->headers_out.status_line.len = 0;
+            }
+        }
+    }
+
      rc = xsltSaveResultToString(&buf, &len, doc, sheet[i - 1].stylesheet);

      xmlFreeDoc(doc);



More information about the nginx-devel mailing list