Hello everyone,

I'm new to OpenBSD and the tech@ list.

I need to run a WordPress-like URL rewriting scheme for my blog. Basically
URLs of the form /long-fancy-blog-post-title/ to be translated to
/index.php?q=long-fancy-blog-post-title.

I couldn't find this function in OpenBSD's new httpd which otherwise seems
like a great piece of s/w.

I spotted however the "block return code return_uri" directive  which
corresponds almost perfectly to my needs. I concluded that extending it to
support code 200 which would signify an internal HTTP redirection is
logical, as the original request is still _blocked_.

The patch attached contains the necessary changes, whereas the httpd.conf
and rewr.php illustrate how the functionality is used.

Could I please ask the person responsible for httpd to integrate my patch
or something with equivalent functionality?

Thank you for your time.

Best regards,

Stanislaw Adaszewski

? httpd
? httpd_url_rewrite.patch
? parse.c
? y.tab.h
Index: parse.y
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/parse.y,v
retrieving revision 1.73
diff -u -p -u -r1.73 parse.y
--- parse.y 19 Jul 2015 05:17:27 -0000 1.73
+++ parse.y 13 Nov 2015 08:29:10 -0000
@@ -907,7 +907,7 @@ filter : block RETURN NUMBER optstring

  if ($4 != NULL) {
  /* Only for 3xx redirection headers */
- if ($3 < 300 || $3 > 399) {
+ if ($3 != 200 && ($3 < 300 || $3 > 399)) {
  yyerror("invalid return code for "
     "location URI");
  free($4);
Index: server_http.c
===================================================================
RCS file: /cvs/src/usr.sbin/httpd/server_http.c,v
retrieving revision 1.96
diff -u -p -u -r1.96 server_http.c
--- server_http.c 31 Jul 2015 00:10:51 -0000 1.96
+++ server_http.c 13 Nov 2015 08:29:10 -0000
@@ -1054,6 +1054,7 @@ server_response(struct httpd *httpd, str
  int portval = -1, ret;
  char *hostval;
  const char *errstr = NULL;
+ char buf[IBUF_READ_SIZE];

  /* Canonicalize the request path */
  if (desc->http_path == NULL ||
@@ -1154,11 +1155,44 @@ server_response(struct httpd *httpd, str
  resp->http_method = desc->http_method;
  if ((resp->http_version = strdup(desc->http_version)) == NULL)
  goto fail;
+
+#ifdef DEBUG
+ DPRINTF("%s: http_path: %s, http_path_alias: %s\n", __func__,
desc->http_path, desc->http_path_alias);
+#endif

  /* Now search for the location */
  srv_conf = server_getlocation(clt, desc->http_path);

- if (srv_conf->flags & SRVFLAG_BLOCK) {
+
+ if ((srv_conf->flags & SRVFLAG_BLOCK) && srv_conf->return_code == 200) {
+#ifdef DEBUG
+ DPRINTF("%s: Rewrite from: %s to %s\n", __func__, desc->http_path,
srv_conf->return_uri);
+#endif
+ if (desc->http_path != NULL) {
+ free(desc->http_path);
+ desc->http_path = NULL;
+ }
+ if (server_expand_http(clt, srv_conf->return_uri, buf, sizeof(buf)) ==
NULL) {
+ server_abort_http(clt, 500, strerror(errno));
+ return (-1);
+ }
+ desc->http_path = strdup(buf);
+ if (desc->http_path == NULL) {
+ server_abort_http(clt, 500, strerror(errno));
+ }
+ desc->http_query = strchr(desc->http_path, '?');
+ if (desc->http_query != NULL) {
+ *desc->http_query++ = '\0';
+ desc->http_query = strdup(desc->http_query);
+ if (desc->http_query == NULL) {
+ server_abort_http(clt, 500, strerror(errno));
+ }
+ }
+ srv_conf = server_getlocation(clt, desc->http_path); // again
+ }
+
+
+ if ((srv_conf->flags & SRVFLAG_BLOCK) && srv_conf->return_code != 200) {
  server_abort_http(clt, srv_conf->return_code,
     srv_conf->return_uri);
  return (-1);


server "default" {
    listen on * port 80
    root "/htdocs/sadaszew/algoholic"

    location "*.php" {
        fastcgi socket "/run/php-fpm.sock"
    }

    location match "^/([%l%u%d%-]+)/$" {
        block return 200 "/rewr.php?q=%1"
    }
}


<?php

echo(sprintf("rewr, q=%s\n", $_GET['q']));

?>

Reply via email to