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

6 Commits   0 Branches   2 Tags

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <sys/sysinfo.h>
#include <sys/types.h>
#include <stdint.h>
#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 <unistd.h>

#include <defs.h>
#include <fatal.h>
#include <http.h>


void fatal( const char *fmt, ... )
{
  va_list arg_ptr;
  char  buf[HTTP_ERRMSG_SIZE];
  char  msg[HTTP_ERRMSG_SIZE];
  char *format = "%s: %s\n";

  va_start( arg_ptr, fmt );

  vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr );

  va_end( arg_ptr ); /* Reset variable arguments. */

  snprintf( buf, HTTP_ERRMSG_SIZE, format, PROGRAM_CGI, msg );

  (void)write( STDERR_FILENO, buf, strlen( buf ) );

  exit( 1 );
}

void fatal_json( const char *fmt, ... )
{
  va_list arg_ptr;

  char  resp[HTTP_ERRMSG_SIZE];
  char  json[HTTP_ERRMSG_SIZE];
  char   msg[HTTP_ERRMSG_SIZE];

  char *http_format = "Status: 500 Internal Server Error\n"
                      "Date: %s\n"
                      "Cache-Control: no-cache, no-store\n"
                      "Content-Type: application/json\n"
                      "Content-Length: %d\n\n"
                      "%s";

  va_start( arg_ptr, fmt );

  vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr );

  va_end( arg_ptr ); /* Reset variable arguments. */

  snprintf( json, HTTP_ERRMSG_SIZE, "{\"error\":\"500\",\"description\":\"Internal Server Error\",\"message\":\"%s: %s\"}", PROGRAM_CGI, msg );

  snprintf( resp, HTTP_ERRMSG_SIZE, http_format,
            http_date( time(NULL) ), strlen( json ), json );

  (void)write( STDOUT_FILENO, resp, strlen( resp ) );

  exit( 1 );
}

void fatal_html( const char *fmt, ... )
{
  va_list arg_ptr;

  char  resp[HTTP_ERRMSG_SIZE];
  char  html[HTTP_ERRMSG_SIZE];
  char   msg[HTTP_ERRMSG_SIZE];

  char *http_format = "Status: 500 Internal Server Error\n"
                      "Date: %s\n"
                      "Cache-Control: no-cache, no-store\n"
                      "Content-Type: text/html; charset=utf-8\n"
                      "Content-Length: %d\n\n"
                      "%s";

  char *html_format =
    "<!DOCTYPE html>\n"
    "<html lang=\"en-US\">\n"
    "  <head>\n"
    "    <meta charset=\"utf-8\">\n"
    "    <!--[if IE]><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"><![endif]-->\n"
    "    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n"
    "    <title>HTTP/%s 500 Internal Server Error</title>\n"
    "    <style>\n"
    "      body {\n"
    "        min-width: 460px;\n"
    "      }\n"
    "      .csvn-internal-server-error {\n"
    "        margin: 48px auto;\n"
    "        padding: 24px 48px 28px 48px;\n"
    "        width: 70%%;\n"
    "        color: #fff;\n"
    "        background: #db2828;\n"
    "        box-shadow: 3px 3px 10px rgba(0,0,0,0.5);\n"
    "        border-radius: 4px;\n"
    "      }\n"
    "      .csvn-internal-server-error h1 {\n"
    "        margin-bottom: 2px;\n"
    "        font-family: sans-serif;\n"
    "        font-size: 28px;\n"
    "      }\n"
    "      .csvn-internal-server-error p.http-date {\n"
    "        margin-top: 2px;\n"
    "        padding-left: 0px 0px 12px 0px;\n"
    "        color: #fc0;\n"
    "        font-family: monospace;\n"
    "        font-size: 18px;\n"
    "      }\n"
    "      .csvn-internal-server-error p {\n"
    "        padding-left: 1px;\n"
    "        font-family: monospace;\n"
    "        font-size: 14px;\n"
    "      }\n"
    "      @media screen and (max-width: 680px) {\n"
    "        .csvn-internal-server-error {\n"
    "          padding: 24px 28px 24px 28px;\n"
    "          width: 80%%;\n"
    "        }\n"
    "        .csvn-internal-server-error h1 {\n"
    "          font-size: 22px;\n"
    "        }\n"
    "        .csvn-internal-server-error p.http-date {\n"
    "          font-size: 16px;\n"
    "        }\n"
    "        .csvn-internal-server-error p {\n"
    "          font-size: 12px;\n"
    "        }\n"
    "      }\n"
    "    </style>\n"
    "  </head>\n"
    "  <body>\n"
    "    <div class=\"csvn-internal-server-error\">\n"
    "      <h1>HTTP/%s 500 Internal Server Error</h1>\n"
    "      <p class=\"http-date\">Date: %s</p>\n"
    "      <p>%s: %s</p>\n"
    "    </div>\n"
    "  </body>\n"
    "</html>\n";

  va_start( arg_ptr, fmt );

  vsnprintf( msg, HTTP_ERRMSG_SIZE, (const void *)fmt, arg_ptr );

  va_end( arg_ptr ); /* Reset variable arguments. */

  snprintf( html, HTTP_ERRMSG_SIZE, html_format,
            HTTP_VERSION, HTTP_VERSION,
            http_date( time(NULL) ), PROGRAM_CGI, msg );

  snprintf( resp, HTTP_ERRMSG_SIZE, http_format,
            http_date( time(NULL) ), strlen( html ), html );

  (void)write( STDOUT_FILENO, resp, strlen( resp ) );

  exit( 1 );
}