Example #1
0
File: cgit.c Project: 5victor/cgit
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;
}
Example #2
0
/*
 * Route request handling to the appropriate handlers
 *
 */
static int process_request(sp_session *session, struct request *req) {
	int now = get_millisecs();

	if(session->connectionstate != SP_CONNECTION_STATE_LOGGED_IN
		&& (req->type != REQ_TYPE_LOGIN && req->type != REQ_TYPE_LOGOUT)) {
		if(req->state == REQ_STATE_NEW) {
			DSFYDEBUG("Postponing request <type %s, state %s, input %p> 10 seconds due to not logged in\n",
					REQUEST_TYPE_STR(req->type), REQUEST_STATE_STR(req->state), req->input);
			req->next_timeout = now + 10*1000;

			return 0;
		}
		else if(req->state == REQ_STATE_RUNNING) {
			DSFYDEBUG("Failing request <type %s, state %s, input %p> due to not logged in\n",
					REQUEST_TYPE_STR(req->type), REQUEST_STATE_STR(req->state), req->input);
			return request_set_result(session, req, SP_ERROR_OTHER_TRANSIENT, NULL);
		}
	}
	else if(req->state == REQ_STATE_NEW && session->num_channels >= 16) {
		DSFYDEBUG("%d channels active, postponing request <type %s, state %s, input %p>\n",
			  session->num_channels, REQUEST_TYPE_STR(req->type),
			  REQUEST_STATE_STR(req->state), req->input);

		if(req->next_timeout < now + 100)
			req->next_timeout = now + 100;
		
		return 0;
	}

	
	switch(req->type) {
	case REQ_TYPE_LOGIN:
		return process_login_request(session, req);
		break;

	case REQ_TYPE_LOGOUT:
		return process_logout_request(session, req);
		break;
	
	case REQ_TYPE_PC_LOAD:
	case REQ_TYPE_PLAYLIST_LOAD:
	case REQ_TYPE_PLAYLIST_CHANGE:
		return playlist_process(session, req);
		break;
	
	case REQ_TYPE_TOPLISTBROWSE:
		return toplistbrowse_process_request(session, req);
		break;

	case REQ_TYPE_SEARCH:
		return search_process_request(session, req);
		break;

	case REQ_TYPE_USER:
		return user_process_request(session, req);
		break;

	case REQ_TYPE_IMAGE:
		return osfy_image_process_request(session, req);
		break;

	case REQ_TYPE_ALBUMBROWSE:
	case REQ_TYPE_ARTISTBROWSE:
	case REQ_TYPE_BROWSE_ALBUM:
	case REQ_TYPE_BROWSE_ARTIST:
	case REQ_TYPE_BROWSE_PLAYLIST_TRACKS:
	case REQ_TYPE_BROWSE_TRACK:
		return browse_process(session, req);
		break;

	case REQ_TYPE_PLAYER_KEY:
	case REQ_TYPE_PLAYER_SUBSTREAM:
	case REQ_TYPE_PLAY_TOKEN_ACQUIRE:
	case REQ_TYPE_PLAY_TOKEN_LOST:
		return player_process_request(session, req);
		break;

	case REQ_TYPE_CACHE_PERIODIC:
		return cache_process(session, req);
		break;

	default:
		DSFYDEBUG("BUG: Unhandled request type '%s'\n", REQUEST_TYPE_STR(req->type));
		break;
	}

	return 0;
}