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