int main(int argc, const char **argv) { const char *path; char *qry; int err, ttl; prepare_context(&ctx); cgit_repolist.length = 0; cgit_repolist.count = 0; cgit_repolist.repos = NULL; cgit_parse_args(argc, argv); parse_configfile(expand_macros(ctx.env.cgit_config), config_cb); ctx.repo = NULL; http_parse_querystring(ctx.qry.raw, querystring_cb); /* If virtual-root isn't specified in cgitrc, lets pretend * that virtual-root equals SCRIPT_NAME, minus any possibly * trailing slashes. */ if (!ctx.cfg.virtual_root && ctx.cfg.script_name) { ctx.cfg.virtual_root = trim_end(ctx.cfg.script_name, '/'); if (!ctx.cfg.virtual_root) ctx.cfg.virtual_root = ""; } /* If no url parameter is specified on the querystring, lets * use PATH_INFO as url. This allows cgit to work with virtual * urls without the need for rewriterules in the webserver (as * long as PATH_INFO is included in the cache lookup key). */ path = ctx.env.path_info; if (!ctx.qry.url && path) { if (path[0] == '/') path++; ctx.qry.url = xstrdup(path); if (ctx.qry.raw) { qry = ctx.qry.raw; ctx.qry.raw = xstrdup(fmt("%s?%s", path, qry)); free(qry); } else ctx.qry.raw = xstrdup(ctx.qry.url); cgit_parse_url(ctx.qry.url); } ttl = calc_ttl(); ctx.page.expires += ttl * 60; if (ctx.env.request_method && !strcmp(ctx.env.request_method, "HEAD")) ctx.cfg.nocache = 1; if (ctx.cfg.nocache) ctx.cfg.cache_size = 0; err = cache_process(ctx.cfg.cache_size, ctx.cfg.cache_root, ctx.qry.raw, ttl, process_request, &ctx); if (err) cgit_print_error(fmt("Error processing page: %s (%d)", strerror(err), err)); return err; }
/** Parse the initial request line * Also modify data of client (padding NULL) * @return position of the last char parsed * NULL on error */ static char *http_parse_request(struct request_t *self, char *data, int len) { char *end; /* method */ end = memchr(data, ' ', len); if (end == NULL) { A_ERR("no method %d: %.*s", len, len, data); return NULL; } *end = '\0'; self->method = data; /* url */ len -= end - data + 1; if (len <= 0) { A_ERR("no url %d", len); return NULL; } data = end + 1; if (*data != '/') { A_ERR("url does not start with / %.*s", len, data); return NULL; } end = memchr(data + 1, ' ', len); /* already know the first char */ if (end == NULL) { A_ERR("no url %d: %.*s", len, len, data); return NULL; } *end = '\0'; self->url = data; /* query string in url */ self->query_string = http_parse_querystring(self->url); /* version */ len -= end - data + 1; if (len <= 0) { A_ERR("no version %d", len); return NULL; } data = end + 1; end = memchr(data, '\n', len); if (end == NULL) { A_ERR("no version %d: %.*s", len, len, data); return NULL; } if (*(end - 1) == '\r') { /* \r\n */ *(end - 1) = '\0'; } else { *end = '\0'; } self->version = data; return end + 1; }