cSvn-UI for SVN Repositories

cGit-UI – is a web interface for Subversion (SVN) Repositories. cSvn CGI script is writen in C and therefore it's fast enough

15 Commits   0 Branches   1 Tag
Index: ui-shared.c
===================================================================
--- ui-shared.c	(nonexistent)
+++ ui-shared.c	(revision 5)
@@ -0,0 +1,1018 @@
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/sysinfo.h>
+#include <sys/types.h>
+#ifdef HAVE_INTTYPES_H
+#include <inttypes.h>
+#else
+#include <stdint.h>
+#endif
+#include <stddef.h>   /* offsetof(3) */
+#include <dirent.h>
+#include <sys/stat.h> /* chmod(2)    */
+#include <sys/file.h>
+#include <sys/mman.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <string.h>   /* strdup(3)   */
+#include <libgen.h>   /* basename(3) */
+#include <ctype.h>    /* tolower(3)  */
+#include <errno.h>
+#include <time.h>
+#include <sys/time.h>
+#include <pwd.h>
+#include <grp.h>
+#include <stdarg.h>
+#include <locale.h>
+#include <math.h>
+#include <unistd.h>
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+#include <magic.h>
+
+#include <nls.h>
+
+#include <defs.h>
+
+#include <fatal.h>
+#include <http.h>
+#include <html.h>
+
+#include <dlist.h>
+#include <strbuf.h>
+#include <repolist.h>
+#include <wrapper.h>
+#include <system.h>
+#include <date.h>
+
+#include <ctx.h>
+#include <ui-shared.h>
+
+
+void csvn_change_location( const char *location )
+{
+  if( ctx.env.no_http && !strcmp( ctx.env.no_http, "1" ) )
+    return;
+
+  htmlf( "Status: %d %s\n", 302, "Found" );
+  htmlf( "Location: %s\n", location );
+  html( "\n" );
+
+  exit( 0 );
+}
+
+void csvn_print_http_headers( void )
+{
+  if( ctx.env.no_http && !strcmp( ctx.env.no_http, "1" ) )
+    return;
+
+  /*****************************************************
+    If we pass 'Status: 200 OK' header then nginx+uWsgi
+    will duplicate other headers randomly 
+   */
+  if( ctx.page.status != 200 )
+    htmlf( "Status: %d %s\n", ctx.page.status, ctx.page.status_message );
+
+  if( ctx.page.mimetype && ctx.page.charset )
+    htmlf( "Content-Type: %s; charset=%s\n", ctx.page.mimetype, ctx.page.charset );
+  else if( ctx.page.mimetype )
+    htmlf( "Content-Type: %s\n", ctx.page.mimetype );
+
+  if( ctx.page.size )
+    htmlf( "Content-Length: %zd\n", ctx.page.size );
+
+  if( !ctx.env.authenticated )
+    html( "Cache-Control: no-cache, no-store\n" );
+
+  htmlf( "Last-Modified: %s\n", http_date( ctx.page.modified ) );
+  htmlf( "Expires: %s\n", http_date( ctx.page.expires ) );
+
+  html( "\n" );
+}
+
+const struct date_mode *csvn_date_mode( enum date_mode_type type )
+{
+  static struct date_mode mode;
+  mode.type = type;
+  mode.local = 0; /* may be from ctx.cfg.local_time; =0/1 */
+  return &mode;
+}
+/* OR data_mode_from_type():
+   struct date_mode *mode = DATE_MODE( DATE_ISO8601 );
+   but in this case mode.local is always zero!
+ */
+
+static void print_rel_date( struct strbuf *sb, time_t t, int tz,
+                            const char *class, const char *value )
+{
+  if( !sb || !t ) return;
+
+  strbuf_addf( sb, "<span class='%s' title='", class );
+  show_date( sb, t, tz, csvn_date_mode( DATE_ISO8601 ) );
+  strbuf_addf( sb, "'>%s</span>", value );
+}
+
+void csvn_print_age( struct strbuf *sb, time_t t, int tz, time_t max_relative )
+{
+  time_t now, secs;
+  char buf[32] = { 0 };
+  size_t val;
+
+  if( !sb || !t ) return;
+
+  now = time( NULL );
+  secs = now - t;
+  if( secs < 0 )
+    secs = 0;
+
+  if( secs > max_relative && max_relative >= 0 )
+  {
+
+    strbuf_addstr( sb, "<span title='" );
+    show_date( sb, t, tz, csvn_date_mode( DATE_ISO8601 ) );
+    strbuf_addstr( sb, "'>" );
+    show_date( sb, t, tz, csvn_date_mode( DATE_SHORT ) );
+    strbuf_addstr( sb, "</span>" );
+    return;
+  }
+
+  if( secs < TM_HOUR * 2 )
+  {
+    val = (size_t)round(secs * 1.0 / TM_MIN);
+    snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" minute", "%"PRIdMAX" minutes", val), val );
+    print_rel_date( sb, t, tz, "age-mins", (const char *)&buf[0] );
+    return;
+  }
+  if( secs < TM_DAY * 2 )
+  {
+    val = (size_t)round(secs * 1.0 / TM_HOUR);
+    snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" hour", "%"PRIdMAX" hours", val), val );
+    print_rel_date( sb, t, tz, "age-hours", (const char *)&buf[0] );
+    return;
+  }
+  if( secs < TM_WEEK * 2 )
+  {
+    val = (size_t)round(secs * 1.0 / TM_DAY);
+    snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" day", "%"PRIdMAX" days", val), val );
+    print_rel_date( sb, t, tz, "age-days", (const char *)&buf[0] );
+    return;
+  }
+  if( secs < TM_MONTH * 2 )
+  {
+    val = (size_t)round(secs * 1.0 / TM_WEEK);
+    snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" week", "%"PRIdMAX" weeks", val), val );
+    print_rel_date( sb, t, tz, "age-weeks", (const char *)&buf[0] );
+    return;
+  }
+  if( secs < TM_YEAR * 2 )
+  {
+    val = (size_t)round(secs * 1.0 / TM_MONTH);
+    snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" month", "%"PRIdMAX" months", val), val );
+    print_rel_date( sb, t, tz, "age-months", (const char *)&buf[0] );
+    return;
+  }
+
+  val = (size_t)round(secs * 1.0 / TM_YEAR);
+  snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" year", "%"PRIdMAX" years", val), val );
+  print_rel_date( sb, t, tz, "age-years", (const char *)&buf[0] );
+}
+
+void csvn_search_repo( const char *path )
+{
+  struct repo *repo = NULL;
+
+  if( !path || !*path ) return;
+
+  repo = lookup_repo( config, path );
+  if( repo )
+  {
+    int position = repo_position( config, repo );
+    if( position != -1 )
+    {
+      char location[1024] = { 0 };
+
+      sprintf( (char *)&location[0], "/?ofs=%d", position );
+      csvn_change_location( (const char *)&location[0] );
+    }
+  }
+}
+
+size_t csvn_repo_last_changed_revision( struct strbuf *sb, struct repo *repo )
+{
+  struct variable  ro_prefix = { (unsigned char *)"checkout-prefix-readonly", { 0 }, DT_PATH };
+  struct variable *prefix = NULL;
+
+  if( !repo || !repo->path ) return 0;
+
+  prefix = lookup( repo, &ro_prefix );
+  if( prefix )
+  {
+    char cmd[1024];
+    pid_t p = (pid_t) -1;
+    int   rc;
+
+    snprintf( (char *)&cmd[0], 1024,
+              "svn info --show-item last-changed-revision --no-newline %s/%s/ 2>/dev/null",
+              prefix->_v.vptr, repo->path );
+    p = sys_exec_command( sb, cmd );
+    rc = sys_wait_command( p, NULL );
+    if( rc != 0 )
+    {
+      return 0;
+    }
+    strbuf_trim( sb );
+    return sb->len;
+  }
+  return 0;
+}
+
+time_t csvn_repo_last_changed_time( struct repo *repo )
+{
+  struct variable  ro_prefix = { (unsigned char *)"checkout-prefix-readonly", { 0 }, DT_PATH };
+  struct variable *prefix = NULL;
+  time_t ret = -1;
+
+  if( !repo || !repo->path ) return ret;
+
+  prefix = lookup( repo, &ro_prefix );
+  if( prefix )
+  {
+    char cmd[1024];
+    struct tm tm;
+    time_t    time = -1;
+    struct strbuf buf = STRBUF_INIT;
+    pid_t p = (pid_t) -1;
+    int   rc;
+
+    snprintf( (char *)&cmd[0], 1024,
+              "svn info --show-item last-changed-date --no-newline %s/%s/ 2>/dev/null",
+              prefix->_v.vptr, repo->path );
+    p = sys_exec_command( &buf, cmd );
+    rc = sys_wait_command( p, NULL );
+    if( rc != 0 )
+    {
+      strbuf_release( &buf );
+      return ret;
+    }
+
+    strbuf_trim( &buf );
+    time = parse_date( &tm, (const char *)buf.buf );
+
+    if( time != -1 )
+    {
+      strbuf_release( &buf );
+      return time;
+    }
+
+    strbuf_release( &buf );
+  }
+  return ret;
+}
+
+
+static xmlNode *xml_find_node_by_name( xmlNode *root, const char *name )
+{
+  xmlNode *node = NULL;
+  xmlNode *ret  = NULL;
+
+  if( !name || !*name || !root || !root->children ) return ret;
+
+  for( node = root->children; node; node = node->next )
+  {
+    if( node->type == XML_ELEMENT_NODE )
+    {
+      if( !strcmp( (char *)name, (char *)node->name ) )
+      {
+        return node;
+      }
+    }
+  }
+  return ret;
+}
+
+static void xml_csvn_info( struct strbuf *sb, struct csvn_info *info )
+{
+  xmlDocPtr doc = NULL;
+  xmlNode *root = NULL;
+
+  if( !sb || !sb->len || !info ) return;
+
+  LIBXML_TEST_VERSION
+
+  doc = xmlReadMemory( sb->buf, sb->len, "info.xml", NULL, 0 );
+  if( !doc )
+  {
+    html_fatal( "cannot parse svn info.xml" );
+    return;
+  }
+
+  root = xmlDocGetRootElement( doc );
+  if( !root )
+  {
+    xmlFreeDoc( doc );
+    xmlCleanupParser();
+    return;
+  }
+
+  if( !strcmp( "info", (char *)root->name ) )
+  {
+    xmlNode *entry = NULL, *commit  = NULL, *cnode   = NULL;
+    xmlChar *content = NULL;
+
+    entry = xml_find_node_by_name( root, "entry" );
+    if( !entry )
+    {
+      xmlFreeDoc( doc );
+      xmlCleanupParser();
+      return;
+    }
+    content = xmlGetProp( entry, (const unsigned char *)"kind" );
+    if( !content )
+    {
+      xmlFreeDoc( doc );
+      xmlCleanupParser();
+      return;
+    }
+    {
+      if( !strcmp( (char *)content, "dir" ) )
+        info->kind = KIND_DIR;
+      else if( !strcmp( (char *)content, "file" ) )
+        info->kind = KIND_FILE;
+      else
+        info->kind = KIND_UNKNOWN;
+    }
+    xmlFree( content );
+
+    commit = xml_find_node_by_name( entry, "commit" );
+    if( !commit )
+    {
+      xmlFreeDoc( doc );
+      xmlCleanupParser();
+      return;
+    }
+    content = xmlGetProp( commit, (const unsigned char *)"revision");
+    if( !content )
+    {
+      xmlFreeDoc( doc );
+      xmlCleanupParser();
+      return;
+    }
+    info->revision = atoi( (const char *)content );
+    xmlFree( content );
+
+    for( cnode = commit->children; cnode; cnode = cnode->next )
+    {
+      if( cnode->type == XML_ELEMENT_NODE )
+      {
+        if( !strcmp( "author", (char *)cnode->name ) )
+        {
+          content = xmlNodeGetContent( cnode );
+          if( !content ) continue;
+          {
+            int   len = (int)strlen( (const char *)content ) + 1;
+            char *author = (char *)__sbrk( len );
+            memcpy( (void *)author, (const void *)content, len );
+            info->author = (const char *)author;
+          }
+          xmlFree( content );
+        }
+        if( !strcmp( "date", (char *)cnode->name ) )
+        {
+          content = xmlNodeGetContent( cnode );
+          if( !content ) continue;
+          {
+            struct tm tm;
+            time_t    time = -1;
+
+            time = parse_date( &tm, (const char *)content );
+            info->date = time;
+          }
+          xmlFree( content );
+        }
+      }
+    } /* End for( cnode = commit->children; cnode; cnode = cnode->next ) */
+  }
+
+  xmlFreeDoc(doc);
+  xmlCleanupParser();
+}
+
+void csvn_repo_info( struct csvn_info *info, int revision )
+{
+  const char *co_prefix = ctx.repo.checkout_ro_prefix;
+  const char *name      = ctx.repo.name;
+  const char *repo_root = ctx.repo.repo_root;
+
+  if( co_prefix )
+  {
+    char repo_path[PATH_MAX] = { 0 };
+    char cmd[PATH_MAX];
+    struct strbuf buf = STRBUF_INIT;
+    pid_t p = (pid_t) -1;
+    int   rc;
+
+    if( repo_root && *repo_root )
+    {
+      strcat( (char *)&repo_path[0], repo_root );
+      strcat( (char *)&repo_path[0], "/" );
+    }
+    strcat( (char *)&repo_path[0], name );
+
+    if( revision )
+      snprintf( (char *)&cmd[0], 1024,
+                "svn info --revision %d --xml %s/%s/ 2>/dev/null",
+                revision, co_prefix, (char *)&repo_path[0] );
+    else
+      snprintf( (char *)&cmd[0], 1024,
+                "svn info --xml %s/%s/ 2>/dev/null",
+                co_prefix, (char *)&repo_path[0] );
+    p = sys_exec_command( &buf, cmd );
+    rc = sys_wait_command( p, NULL );
+    if( rc != 0 )
+    {
+      strbuf_release( &buf );
+      return;
+    }
+
+    xml_csvn_info( &buf, info );
+    strbuf_release( &buf );
+  }
+  return;
+}
+
+
+static const char *lang_info_by_path( const char *path )
+{
+  char *lang = NULL, *name = NULL, *ext = NULL;
+
+  if( !path || !*path ) return (const char *)lang;
+
+  ext = rindex( path, '.' );
+  if( ext )
+  {
+    if( !strncasecmp( ext, ".mk", 3 )       ||
+        !strncasecmp( ext, ".make", 5 )     ||
+        !strncasecmp( ext, ".Makefile", 9 )   )
+    {
+      lang = (char *)__sbrk( 9 );
+      sprintf( lang, "Makefile" );
+    }
+    else if( !strncasecmp( ext, ".md", 3 ) )
+    {
+      lang = (char *)__sbrk( 9 );
+      sprintf( lang, "Markdown" );
+    }
+    else if( !strncasecmp( ext, ".diff", 5 ) ||
+             !strncasecmp( ext, ".patch", 6 )  )
+    {
+      lang = (char *)__sbrk( 9 );
+      sprintf( lang, "Diff" );
+    }
+    else if( !strncasecmp( ext, ".c", 2 ) ||
+             !strncasecmp( ext, ".h", 2 )   )
+    {
+      lang = (char *)__sbrk( 2 );
+      sprintf( lang, "C" );
+    }
+    else if( !strncasecmp( ext, ".cpp", 4 ) ||
+             !strncasecmp( ext, ".cxx", 4 ) ||
+             !strncasecmp( ext, ".hpp", 4 )   )
+    {
+      lang = (char *)__sbrk( 4 );
+      sprintf( lang, "C++" );
+    }
+    else if( !strncasecmp( ext, ".html", 5 ) )
+    {
+      lang = (char *)__sbrk( 5 );
+      sprintf( lang, "HTML" );
+    }
+    else if( !strncasecmp( ext, ".css", 4 ) )
+    {
+      lang = (char *)__sbrk( 4 );
+      sprintf( lang, "CSS" );
+    }
+    else if( !strncasecmp( ext, ".js", 3 ) )
+    {
+      lang = (char *)__sbrk( 11 );
+      sprintf( lang, "javascript" );
+    }
+    else if( !strncasecmp( ext, ".json", 5 ) )
+    {
+      lang = (char *)__sbrk( 5 );
+      sprintf( lang, "JSON" );
+    }
+    else if( !strncasecmp( ext, ".less", 5 ) )
+    {
+      lang = (char *)__sbrk( 5 );
+      sprintf( lang, "LESS" );
+    }
+    else if( !strncasecmp( ext, ".scss", 5 ) )
+    {
+      lang = (char *)__sbrk( 5 );
+      sprintf( lang, "SCSS" );
+    }
+    else if( !strncasecmp( ext, ".pl", 3 ) ||
+             !strncasecmp( ext, ".pm", 3 )   )
+    {
+      lang = (char *)__sbrk( 5 );
+      sprintf( lang, "Perl" );
+    }
+    else if( !strncasecmp( ext, ".py", 3 ) )
+    {
+      lang = (char *)__sbrk( 7 );
+      sprintf( lang, "Python" );
+    }
+    else if( !strncasecmp( ext, ".php", 4 ) )
+    {
+      lang = (char *)__sbrk( 4 );
+      sprintf( lang, "PHP" );
+    }
+    else if( !strncasecmp( ext, ".txt", 4 ) )
+    {
+      lang = (char *)__sbrk( 10 );
+      sprintf( lang, "plaintext" );
+    }
+    else if( !strncasecmp( ext, ".go", 3 ) )
+    {
+      lang = (char *)__sbrk( 3 );
+      sprintf( lang, "Go" );
+    }
+  }
+
+  name = rindex( path, '/' );
+  if( name )
+  {
+    ++name;
+    if( !strncasecmp( name, "Makefile", 8 ) )
+    {
+      lang = (char *)__sbrk( 9 );
+      sprintf( lang, "Makefile" );
+    }
+    else if( !strcasecmp( name, "README" ) )
+    {
+      lang = (char *)__sbrk( 10 );
+      sprintf( lang, "Plaintext" );
+    }
+    else if( !strcasecmp( name, "LICENSE" ) )
+    {
+      lang = (char *)__sbrk( 10 );
+      sprintf( lang, "Plaintext" );
+    }
+  }
+
+  /***************************
+    all chances are exhausted. probably highlight.js do it correctly...
+   */
+  /*
+  if( !lang )
+  {
+    lang = (char *)__sbrk( 10 );
+    sprintf( lang, "plaintext" );
+  }
+  */
+
+  return (const char *)lang;
+}
+
+static const char *lang_info( const char *mime_type, const char *path )
+{
+  char *lang = NULL;
+
+  if( !mime_type ) return (const char *)lang;
+
+  if( !strncmp( mime_type, "text/", 5 ) )
+  {
+    if( !strncmp( mime_type, "text/x-c", 8 ) || !strncmp( mime_type, "text/x-csrc", 11 ) )
+    {
+      lang = (char *)__sbrk( 2 );
+      sprintf( lang, "C" );
+    }
+    else if( !strncmp( mime_type, "text/x-diff", 11 ) )
+    {
+      lang = (char *)__sbrk( 5 );
+      sprintf( lang, "Diff" );
+    }
+    else if( !strncmp( mime_type, "text/x-makefile", 15 ) )
+    {
+      lang = (char *)__sbrk( 9 );
+      sprintf( lang, "Makefile" );
+    }
+    else if( !strncmp( mime_type, "text/x-shell", 12 ) )
+    {
+      lang = (char *)__sbrk( 5 );
+      sprintf( lang, "Bash" );
+    }
+
+    /*
+      The 'text/plain;' mime type does not mean that the file contains plain text.
+      But we have to identify the language:
+     */
+    if( !lang && path )
+    {
+      /* Here we can make lang identification by filename or extension */
+      lang = (char *)lang_info_by_path( path );
+    }
+  }
+
+  return (const char *)lang;
+}
+
+static const char *mime_info( struct csvn_info *info, const char *buffer, size_t length )
+{
+  const char *mime = NULL;
+  magic_t     magic;
+
+  if( !info || !buffer || !length ) return mime;
+
+  magic = magic_open( MAGIC_MIME );
+  if( !magic )
+  {
+    html_fatal( "unable to initialize magic library" );
+    return mime;
+  }
+  if( magic_load( magic, NULL ) != 0 )
+  {
+    html_fatal( "cannot load magic database - %s\n", magic_error( magic ) );
+    magic_close( magic );
+    return mime;
+  }
+
+  mime = magic_buffer( magic, buffer, length );
+  if( mime )
+  {
+    int len = (int)strlen( mime ) + 1;
+    char *mime_type = (char *)__sbrk( len );
+    memcpy( (void *)mime_type, (const void *)mime, len );
+    info->mime = (const char *)mime_type;
+  }
+
+  magic_close( magic );
+  return mime;
+}
+
+void csvn_rpath_mime_info( struct csvn_info *info, const char *relative_path, int revision )
+{
+  const char *co_prefix = ctx.repo.checkout_ro_prefix;
+  const char *name      = ctx.repo.name;
+  const char *repo_root = ctx.repo.repo_root;
+
+  if( !info || !relative_path ) return;
+
+  if( co_prefix )
+  {
+    char repo_path[PATH_MAX] = { 0 };
+    char cmd[PATH_MAX];
+    struct strbuf buf = STRBUF_INIT;
+    pid_t p = (pid_t) -1;
+    int   rc;
+
+    if( repo_root && *repo_root )
+    {
+      strcat( (char *)&repo_path[0], repo_root );
+      strcat( (char *)&repo_path[0], "/" );
+    }
+    strcat( (char *)&repo_path[0], name );
+
+    if( revision )
+      snprintf( (char *)&cmd[0], 1024,
+                "svn cat --revision %d %s/%s/%s 2>/dev/null | tr -d '\\0' | head -c 1024",
+                revision, co_prefix, (char *)&repo_path[0], relative_path );
+    else
+      snprintf( (char *)&cmd[0], 1024,
+                "svn cat %s/%s/%s 2>/dev/null | tr -d '\\0' | head -c 1024",
+                co_prefix, (char *)&repo_path[0], relative_path );
+    p = sys_exec_command( &buf, cmd );
+    rc = sys_wait_command( p, NULL );
+    if( rc != 0 )
+    {
+      strbuf_release( &buf );
+      return;
+    }
+    if( buf.buf[0] )
+    {
+      mime_info( info, (const char *)buf.buf, buf.len );
+      info->lang = lang_info( (const char *)(info->mime), relative_path );
+    }
+    strbuf_release( &buf );
+  }
+  return;
+}
+
+void csvn_rpath_info( struct csvn_info *info, const char *relative_path, int revision )
+{
+  const char *co_prefix = ctx.repo.checkout_ro_prefix;
+  const char *name      = ctx.repo.name;
+  const char *repo_root = ctx.repo.repo_root;
+
+  if( !info || !relative_path ) return;
+
+  if( co_prefix )
+  {
+    char repo_path[PATH_MAX] = { 0 };
+    char cmd[PATH_MAX];
+    struct strbuf buf = STRBUF_INIT;
+    pid_t p = (pid_t) -1;
+    int   rc;
+
+    if( repo_root && *repo_root )
+    {
+      strcat( (char *)&repo_path[0], repo_root );
+      strcat( (char *)&repo_path[0], "/" );
+    }
+    strcat( (char *)&repo_path[0], name );
+
+    if( revision )
+      snprintf( (char *)&cmd[0], 1024,
+                "svn info --revision %d --xml %s/%s/%s 2>/dev/null",
+                revision, co_prefix, (char *)&repo_path[0], relative_path );
+    else
+      snprintf( (char *)&cmd[0], 1024,
+                "svn info --xml %s/%s/%s 2>/dev/null",
+                co_prefix, (char *)&repo_path[0], relative_path );
+    p = sys_exec_command( &buf, cmd );
+    rc = sys_wait_command( p, NULL );
+    if( rc != 0 )
+    {
+      strbuf_release( &buf );
+      return;
+    }
+
+    xml_csvn_info( &buf, info );
+    if( info->kind == KIND_FILE )
+      csvn_rpath_mime_info( info, relative_path, revision );
+    strbuf_release( &buf );
+  }
+  return;
+}
+
+
+static int xml_csvn_dirs_number( const struct strbuf *buf )
+{
+  xmlDocPtr doc = NULL;
+  xmlNode *root = NULL;
+  int count     = 0;
+
+  LIBXML_TEST_VERSION
+
+  doc = xmlReadMemory( buf->buf, buf->len, "list.xml", NULL, 0 );
+  if( !doc )
+  {
+    html_fatal( "cannot parse svn list.xml" );
+    return count;
+  }
+
+  root = xmlDocGetRootElement( doc );
+  if( !root )
+  {
+    xmlFreeDoc( doc );
+    xmlCleanupParser();
+    return count;
+  }
+
+  if( !strcmp( "lists", (char *)root->name ) )
+  {
+    xmlNode *list = NULL, *node = NULL;
+
+    list = xml_find_node_by_name( root, "list" );
+    if( !list )
+    {
+      xmlFreeDoc( doc );
+      xmlCleanupParser();
+      return count;
+    }
+
+    /************************************************
+      Count directories (excluding 'deadwood' name):
+     */
+    for( node = list->children; node; node = node->next )
+    {
+      if( node->type == XML_ELEMENT_NODE && !strcmp( "entry", (char *)node->name ) )
+      {
+        xmlChar *content = NULL;
+        xmlNode *param   = NULL;
+
+        content = xmlGetProp( node, (const unsigned char *)"kind" );
+        if( !strcmp( (const char *)content, "dir" ) )
+        {
+          xmlChar *name = NULL;
+
+          for( param = node->children; param; param= param->next )
+          {
+            if( param->type == XML_ELEMENT_NODE && !strcmp( "name", (char *)param->name ) )
+            {
+              name = xmlNodeGetContent( param );
+            }
+          }
+
+          if( name && strcmp( (char *)name, "deadwood" ) )
+          {
+            count += 1;
+          }
+
+          if( name )
+            xmlFree( name );
+        }
+        xmlFree( content );
+      }
+    }
+  }
+
+  xmlFreeDoc(doc);
+  xmlCleanupParser();
+
+  return count;
+}
+
+
+void csvn_repo_branches_number( struct csvn_repository *rctx )
+{
+  const char *prefix = NULL, *name = NULL, *branches = NULL;
+
+  if( !rctx ) return;
+
+  name     = rctx->name;
+  branches = rctx->branches;
+  prefix   = rctx->checkout_ro_prefix;
+
+  if( prefix && name && branches )
+  {
+    char cmd[1024];
+    struct strbuf buf = STRBUF_INIT;
+    pid_t p = (pid_t) -1;
+    int   rc;
+
+    snprintf( (char *)&cmd[0], 1024, "svn list --xml %s/%s/%s 2>/dev/null", prefix, name, branches );
+    p = sys_exec_command( &buf, cmd );
+    rc = sys_wait_command( p, NULL );
+    if( rc != 0 )
+    {
+      strbuf_release( &buf );
+      return;
+    }
+
+    rctx->nbranches = xml_csvn_dirs_number( (const struct strbuf *)&buf );
+
+    strbuf_release( &buf );
+  }
+  return;
+}
+
+void csvn_repo_tags_number( struct csvn_repository *rctx )
+{
+  const char *prefix = NULL, *name = NULL, *tags = NULL;
+
+  if( !rctx ) return;
+
+  name   = rctx->name;
+  tags   = rctx->tags;
+  prefix = rctx->checkout_ro_prefix;
+
+  if( prefix && name && tags )
+  {
+    char cmd[1024];
+    struct strbuf buf = STRBUF_INIT;
+    pid_t p = (pid_t) -1;
+    int   rc;
+
+    snprintf( (char *)&cmd[0], 1024, "svn list --xml %s/%s/%s 2>/dev/null", prefix, name, tags );
+    p = sys_exec_command( &buf, cmd );
+    rc = sys_wait_command( p, NULL );
+    if( rc != 0 )
+    {
+      strbuf_release( &buf );
+      return;
+    }
+
+    rctx->ntags = xml_csvn_dirs_number( (const struct strbuf *)&buf );
+
+    strbuf_release( &buf );
+  }
+  return;
+}
+
+void csvn_svn_version( struct csvn_versions *vctx )
+{
+  char cmd[1024];
+  struct strbuf buf = STRBUF_INIT;
+  pid_t p = (pid_t) -1;
+  int   rc;
+
+  char *version = NULL;
+  int   len = 0;
+
+  if( !vctx ) return;
+
+  snprintf( (char *)&cmd[0], 1024, "svn --version --quiet 2>/dev/null" );
+  p = sys_exec_command( &buf, cmd );
+  rc = sys_wait_command( p, NULL );
+  if( rc != 0 )
+  {
+    strbuf_release( &buf );
+    return;
+  }
+
+  strbuf_trim( &buf );
+
+  len = (int)strlen( buf.buf ) + 1;
+  version = (char *)__sbrk( len );
+  memcpy( (void *)version, (const void *)buf.buf, (size_t)len );
+  vctx->subversion = (const char *)version;
+
+  strbuf_release( &buf );
+
+  return;
+}
+
+void csvn_nginx_version( struct csvn_versions *vctx )
+{
+  char cmd[1024];
+  struct strbuf buf = STRBUF_INIT;
+  pid_t p = (pid_t) -1;
+  int   rc;
+
+  char *version = NULL;
+  int   len = 0;
+
+  if( !vctx ) return;
+
+  snprintf( (char *)&cmd[0], 1024, "nginx -v 2>&1 | cut -f2 -d'/'" );
+  p = sys_exec_command( &buf, cmd );
+  rc = sys_wait_command( p, NULL );
+  if( rc != 0 )
+  {
+    strbuf_release( &buf );
+    return;
+  }
+
+  if( buf.buf[0] )
+  {
+    strbuf_trim( &buf );
+
+    len = (int)strlen( buf.buf ) + 1;
+    version = (char *)__sbrk( len );
+    memcpy( (void *)version, (const void *)buf.buf, (size_t)len );
+    vctx->nginx = (const char *)version;
+  }
+
+  strbuf_release( &buf );
+
+  return;
+}
+
+
+void csvn_print_404_page( void )
+{
+  FILE  *fp;
+  struct strbuf buf = STRBUF_INIT;
+
+  fp = xfopen( ctx.page.header, "r" );
+  (void)strbuf_env_fread( &buf, fp );
+  fclose( fp );
+
+  strbuf_addf( &buf, "        <div class=\"content segment\">\n" );
+  strbuf_addf( &buf, "          <div class=\"container\">\n" );
+  strbuf_addf( &buf, "            <div class=\"csvn-main-content\">\n" );
+  strbuf_addf( &buf, "              <h1>Requested resource not found</h1>\n" );
+  strbuf_addf( &buf, "              <p class='leading'>Please check the requested URL or try again later.</p>\n" );
+  strbuf_addf( &buf, "            </div> <!-- End of csvn-main-content -->\n" );
+  strbuf_addf( &buf, "          </div> <!-- End of container -->\n" );
+  strbuf_addf( &buf, "        </div> <!-- End of content segment -->\n" );
+
+  fp = xfopen( ctx.page.footer, "r" );
+  (void)strbuf_env_fread( &buf, fp );
+  fclose( fp );
+
+  ctx.page.size = buf.len;
+  csvn_print_http_headers();
+  strbuf_write( &buf, STDOUT_FILENO );
+  strbuf_release( &buf );
+}
+
+
+void csvn_print_raw_file( struct strbuf *sb, const char *mime )
+{
+  struct strbuf buf = STRBUF_INIT;
+  char *p = (char *)mime;
+
+  char *http_format = "Date: %s\n"
+                      "Content-Type: %s\n"
+                      "Content-Length: %ld\n\n";
+
+  if( !sb || !mime ) return;
+
+  while( *p && *p != ';' ) ++p;
+  if( *p ) *p = '\0';
+
+  strbuf_addf( &buf, http_format, http_date( time(NULL) ), mime, sb->len );
+  strbuf_addbuf( &buf, (const struct strbuf *)sb );
+  strbuf_write( &buf, STDOUT_FILENO );
+  strbuf_release( &buf );
+
+  exit( 0 );
+}