Beispiel #1
0
void
wiki_show_changes_page(HttpResponse *res)
{
    WikiPageList **pages = NULL;
    int            n_pages, i;

    wiki_show_header(res, "Changes", FALSE);

    pages = wiki_get_pages(&n_pages, NULL);

    for (i=0; i<n_pages; i++)
    {
        struct tm   *pTm;
        char   datebuf[64];

        pTm = localtime(&pages[i]->mtime);
        strftime(datebuf, sizeof(datebuf), "%Y-%m-%d %H:%M", pTm);
        http_response_printf(res, "<a href='%s'>%s</a> %s<br />\n",
                             pages[i]->name,
                             pages[i]->name,
                             datebuf);
    }

    wiki_show_footer(res);
    http_response_send(res);
    exit(0);
}
Beispiel #2
0
void
wiki_show_search_results_page(HttpResponse *res, char *expr)
{
    WikiPageList **pages = NULL;
    int            n_pages, i;

    if (expr == NULL || strlen(expr) == 0)
    {
        wiki_show_header(res, "Search", FALSE);
        http_response_printf(res, "No Search Terms supplied");
        wiki_show_footer(res);
        http_response_send(res);
        exit(0);
    }

    pages = wiki_get_pages(&n_pages, expr);

    if (pages)
    {
        for (i=0; i<n_pages; i++)
            if (!strcmp(pages[i]->name, expr)) /* redirect on page name match */
                wiki_redirect(res, pages[i]->name);

        wiki_show_header(res, "Search", FALSE);

        for (i=0; i<n_pages; i++)
        {
            http_response_printf(res, "<a href='%s'>%s</a><br />\n",
                                 pages[i]->name,
                                 pages[i]->name);
        }
    }
    else
    {
        wiki_show_header(res, "Search", FALSE);
        http_response_printf(res, "No matches");
    }

    wiki_show_footer(res);
    http_response_send(res);

    exit(0);
}
Beispiel #3
0
void
wiki_show_create_page(HttpResponse *res)
{
    wiki_show_header(res, "Create New Page", FALSE);
    http_response_printf(res, "%s", CREATEFORM);
    wiki_show_footer(res);

    http_response_send(res);
    exit(0);
}
void
wiki_page_forbiden(HttpResponse *res)
{
    http_response_printf(res,
                         "<html>\n<p>Permission denied - You need to login.</p>"
                         "<a href='javascript:javascript:history.go(-1)'>Return to the previous page.</a>\n"
                         "</html>\n");
    http_response_set_status(res, 403, "Forbidden</");
    http_response_send(res);

    exit(0);
}
Beispiel #5
0
void dispatch_request(int socket_fd) {

	static char buffer[BUFSIZE + 1];
	static struct http_request request;

	request.socket_fd = socket_fd;

	long ret = read(socket_fd,buffer,BUFSIZE); 	// read Web request in one go
	if(ret > 0 && ret < BUFSIZE)			// return code is valid chars 
		buffer[ret]=0;				// terminate the buffer
	else buffer[0]=0;

	if(parse_http_request(buffer, &request) == 0) {
		printf("Warning: Could not parse incomming request dumping headers!\n");
		printf("%s", buffer);
		http_response_send("400 Bad Request", "text/plain", &request, "Could not parse HTTP headers");
	}

	struct route *active_route;
	if(routes_match(&request, &active_route) == 0) {
		printf("Warning: 404 on %s\n", request.path);
		http_response_send("404 Not Found", "text/plain", &request, "Resource not found");
	}
	else {
		if(active_route->type == RT_STATIC) {
			printf("Log: Found static route %s\n", active_route->static_content_source);
			http_response_send("200 OK", active_route->content_type, &request, active_route->static_content);
		}
		else if(active_route->type == RT_DYNAMIC) {
			printf("Log: Found dynamic route %s\n", active_route->request_string);
			(*active_route->callback)(&request);
		}
		else {
			printf("Warning: Request method not implemented dumping request\n");
			http_response_send("503 Not Implemented", "text/plain", &request, "Method not implemented");
		}
	}
	destroy_request(&request);
} 
Beispiel #6
0
void
wiki_show_edit_page(HttpResponse *res, char *wikitext, char *page)
{
    wiki_show_header(res, page, FALSE);

    if (wikitext == NULL) wikitext = "";
    http_response_printf(res, EDITFORM, page, wikitext);

    wiki_show_footer(res);

    http_response_send(res);
    exit(0);
}
Beispiel #7
0
int
wiki_redirect(HttpResponse *res, char *location)
{
    int   header_len = strlen(location) + 14;
    char *header = alloca(sizeof(char)*header_len);

    snprintf(header, header_len, "Location: %s\r\n", location);

    http_response_append_header(res, header);
    http_response_printf(res, "<html>\n<p>Redirect to %s</p>\n</html>\n",
                         location);
    http_response_set_status(res, 302, "Moved Temporarily");
    http_response_send(res);

    exit(0);
}
Beispiel #8
0
void
wiki_show_changes_page_rss(HttpResponse *res)
{
    WikiPageList **pages = NULL;
    int            n_pages, i;
    /*char          *html_clean_wikitext = NULL;
      char          *wikitext; */

    pages = wiki_get_pages(&n_pages, NULL);

    http_response_printf(res, "<?xml version=\"1.0\"encoding=\"ISO-8859-1\"?>\n"
                         "<rss version=\"2.0\">\n"
                         "<channel><title>DidiWiki Changes feed</title>\n");

    for (i=0; i<n_pages; i++)
    {
        struct tm   *pTm;
        char         datebuf[64];

        pTm = localtime(&pages[i]->mtime);
        strftime(datebuf, sizeof(datebuf), "%Y-%m-%d %H:%M", pTm);

        http_response_printf(res,
                             "<item><title>%s</title>"
                             "<link>%s%s</link><description>"
                             "Modified %s\n",
                             pages[i]->name,
                             getenv("DIDIWIKI_URL_PREFIX") ? getenv("DIDIWIKI_URL_PREFIX") : "",
                             pages[i]->name,
                             datebuf);

        /*
        wikitext = file_read(pages[i]->name);
        http_response_printf_alloc_buffer(res, strlen(wikitext)*2);
        html_clean_wikitext = util_htmlize(wikitext, strlen(wikitext));
        wiki_print_data_as_html(res, html_clean_wikitext);
        */
        http_response_printf(res, "</description></item>\n");
    }

    http_response_printf(res, "</channel>\n</rss>");

    http_response_send(res);
    exit(0);
}
/*
** Send a reply indicating that the HTTP request was malformed
*/
static void 
malformed_request(int code, char *info)
{
  HttpResponse *res = NULL;

  res = http_response_new(NULL);

  http_response_set_status(res, 501, "Not Implemented");
  http_response_printf(res, 
    "<html><body>Unrecognized HTTP Request Code=%i</body></html>\n",
    code);
  http_response_send(res);
  
  /* log Error */
  syslog(LOG_LOCAL0|LOG_INFO, "Malformed request 501\nCode=%i\n%s\n", code,info);
        

  exit(0);
}
Beispiel #10
0
void
wiki_show_page(HttpResponse *res, char *wikitext, char *page)
{
    char *html_clean_wikitext = NULL;

    http_response_printf_alloc_buffer(res, strlen(wikitext)*2);

    wiki_show_header(res, page, TRUE);

    html_clean_wikitext = util_htmlize(wikitext, strlen(wikitext));

    wiki_print_data_as_html(res, html_clean_wikitext);

    wiki_show_footer(res);

    http_response_send(res);

    exit(0);

}
Beispiel #11
0
void
http_response_send_smallfile
  (HttpResponse *res, char *filename, char *content, unsigned long sizelimit)
/* ! file loaded in mem */
{
  unsigned char *data;
  FILE *fp=fopen(filename,"rb");
  /* get file size and alloc memory */
  fseek (fp , 0 , SEEK_END);
  unsigned long datasize = ftell(fp);
  if (datasize > sizelimit)
    exit(-1);
  rewind (fp);
  if (!(data = (unsigned char*)malloc(datasize)))
    exit(-1);
  /* load the file */
  if (!fread(data, datasize, 1, fp))
    exit(-1);
  http_response_set_content_type(res, content);
  http_response_set_data(res, data, datasize);
  http_response_send(res);
  fclose(fp);
}
Beispiel #12
0
void wiki_show_changes_page_rss(HttpResponse *res)
{
	WikiPageList **pages = NULL;
	int            n_pages, i;

	pages = wiki_get_pages(&n_pages, NULL);

	http_response_set_content_type(res, "application/xhtml+xml");

	http_response_printf(res, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"
	                     "<rss version=\"2.0\">\n"
	                     "<channel><title>DidiWiki Changes feed</title>\n");

	for (i = 0; i < n_pages; i++) {
		struct tm   *pTm;
		char         datebuf[64];

		pTm = localtime(&pages[i]->mtime);
		strftime(datebuf, sizeof(datebuf), "%Y-%m-%d %H:%M", pTm);

		http_response_printf(res,
		                     "<item><title>%s</title>"
		                     "<link>%s%s</link><description>"
		                     "Modified %s\n",
		                     pages[i]->name,
		                     getenv("PORTABLEWIKI_URL_PREFIX") ? getenv("PORTABLEWIKI_URL_PREFIX") : "",
		                     pages[i]->name,
		                     datebuf);

		http_response_printf(res, "</description></item>\n");
	}

	http_response_printf(res, "</channel>\n</rss>");

	http_response_send(res);
	exit(0);
}
Beispiel #13
0
/* Use cmd "diff" */
void wiki_show_diff_between_pages(HttpResponse *res, char *page, int mode)
{
	FILE *fp;
	char line[128];
	char *str_ptr;
	char *cmd;
	int val1, val2, nbln = 1, lg;
	char buffer[128];
	char action;

	if (!page) exit(0);

	char *current = strdup(page);
	*strstr(current, ".prev") = '\0';
	fp = fopen(page, "r");
	lg = asprintf(&cmd, "diff -abB %s %s", page, current);

//	wiki_show_header(res, "Changes", FALSE);

	FILE *pipe = popen(cmd, "r");
	if (!pipe)
		exit(0);

	http_response_printf(res, "<p>Current page: %s</p>\n", current);
	while (!feof(pipe)) {
		if (fgets(buffer, 128, pipe) != NULL) {
			/* Analyze results returned by diff */
			if (strchr("<>=", buffer[0]))
				http_response_printf(res, "<B>%s%s</B><br>\n",
				                     buffer[0] == '>' ? "New:" : "", &buffer[1]);
			else if ((str_ptr = strpbrk(buffer, "acd"))) {
				action = *str_ptr;
				*str_ptr = '\0';
				val1 = atoi(buffer);
				if ((str_ptr = strchr(buffer, ',')))
					val2 = atoi(str_ptr + 1);
				else
					val2 = val1;

				if (mode == 2) {
					/* Show previous page markup */
					while (nbln < val2) {
						if (!fgets(line, 128, fp))
							exit(0);
						http_response_printf(res, "%i:%s<br>\n", nbln, line);
						nbln++;
					}
				}

				/* Explain change */
				switch (action) {
				case 'd':
					http_response_printf(res, "<B>Deleted line %i-%i:<br></B>\n", val1, val2);
					break;
				case 'a':
					http_response_printf(res, "<B>Added line %i-%i:<br></B>\n", val1, val2);
					break;
				case 'c':
					http_response_printf(res, "<B>Changed line %i-%i:<br></B>\n", val1, val2);
					break;
				}
			}
		}
	}
	if (mode == 2) {
		while (fgets(line, 128, fp)) {
			http_response_printf(res, "%i:%s<br>\n", nbln, line);
			nbln++;
		}
	}

	pclose(pipe);
	fclose(fp);

//	wiki_show_footer(res);
	http_response_send(res);
	exit(0);
}
Beispiel #14
0
void wiki_show_changes_page(HttpResponse *res)
{
	WikiPageList **pages = NULL;
	int n_pages, i, j, m, lg;
	char spacing[24];
	char *difflink;
	int *table, *done; //alloc mem with n_pages
	char *str_ptr;

//	wiki_show_header(res, "Changes", FALSE);

	pages = wiki_get_pages(&n_pages, NULL);

	table = malloc((1 + n_pages) * sizeof(int));
	done = malloc((1 + n_pages) * sizeof(int));
    
    http_response_set_content_type(res, "text/html");
	/* regroup file and previous file */
	for (i = 0; i < n_pages + 1; i++) {
		done[i] = 0;
		table[i] = 0;
	}
	m = 0;
	for (i = 0; i < n_pages; i++)
		if (!done[i] && !strstr(pages[i]->name, ".prev.1")) {
			for (j = 0; j < n_pages; j++)
				if (!done[j]
				    && (str_ptr = strstr(pages[j]->name, pages[i]->name))
				    &&  !strcmp(str_ptr + strlen(pages[i]->name), ".prev.1")) {
					table[m++] = i;
					table[m++] = j;
					done[i] = 1;
					done[j] = 1;
					break;
				}
		}
	/* complete with new files */
	for (i = 0; i < n_pages; i++)
		if (!done[i]) table[m++] = i;

	for (j = 0; j < m; j++) {
		struct tm   *pTm;
		char   datebuf[64];
		i = table[j];
		if (strstr(pages[i]->name, ".prev.1")) {
			strcpy(spacing, "&nbsp;&nbsp;");
			lg = asprintf(&difflink,
			              "<a href='/#/Diff/%s'>diff</a>\n"
			              "<a href='/#/Comp/%s'>comp</a>\n",
			              pages[i]->name, pages[i]->name);
		} else {
			*spacing = '\0';
			difflink = strdup("\0");
		}
		pTm = localtime(&pages[i]->mtime);
		strftime(datebuf, sizeof(datebuf), "%Y-%m-%d %H:%M", pTm);
		http_response_printf(res, "%s<a href=/#/View/%s>%s</a> %s %s<br />\n",
		                     spacing,
		                     pages[i]->name,
		                     pages[i]->name,
		                     datebuf,
		                     difflink);
	}

	http_response_printf(res, "<p>Wiki changes are also available as a "
	                     "<a href='/rss'>RSS</a> feed.\n");

//	wiki_show_footer(res);
	http_response_send(res);
	free(table);
	free(done);
	exit(0);
}
Beispiel #15
0
void
wiki_handle_rest_call(HttpRequest  *req,
                      HttpResponse *res,
                      char         *func)
{

    if (func != NULL && *func != '\0')
    {
        if (!strcmp(func, "page/get"))
        {
            char *page = http_request_param_get(req, "page");

            if (page == NULL)
                page = http_request_get_query_string(req);

            if (page && (access(page, R_OK) == 0))
            {
                http_response_printf(res, "%s", file_read(page));
                http_response_send(res);
                return;
            }
        }
        else if (!strcmp(func, "page/set"))
        {
            char *wikitext = NULL, *page = NULL;
            if( ( (wikitext = http_request_param_get(req, "text")) != NULL)
                    && ( (page = http_request_param_get(req, "page")) != NULL))
            {
                file_write(page, wikitext);
                http_response_printf(res, "success");
                http_response_send(res);
                return;
            }
        }
        else if (!strcmp(func, "page/delete"))
        {
            char *page = http_request_param_get(req, "page");

            if (page == NULL)
                page = http_request_get_query_string(req);

            if (page && (unlink(page) > 0))
            {
                http_response_printf(res, "success");
                http_response_send(res);
                return;
            }
        }
        else if (!strcmp(func, "page/exists"))
        {
            char *page = http_request_param_get(req, "page");

            if (page == NULL)
                page = http_request_get_query_string(req);

            if (page && (access(page, R_OK) == 0))
            {
                http_response_printf(res, "success");
                http_response_send(res);
                return;
            }
        }
        else if (!strcmp(func, "pages") || !strcmp(func, "search"))
        {
            WikiPageList **pages = NULL;
            int            n_pages, i;
            char          *expr = http_request_param_get(req, "expr");

            if (expr == NULL)
                expr = http_request_get_query_string(req);

            pages = wiki_get_pages(&n_pages, expr);

            if (pages)
            {
                for (i=0; i<n_pages; i++)
                {
                    struct tm   *pTm;
                    char   datebuf[64];

                    pTm = localtime(&pages[i]->mtime);
                    strftime(datebuf, sizeof(datebuf), "%Y-%m-%d %H:%M", pTm);
                    http_response_printf(res, "%s\t%s\n", pages[i]->name, datebuf);
                }

                http_response_send(res);
                return;
            }
        }
    }

    http_response_set_status(res, 500, "Error");
    http_response_printf(res, "<html><body>Failed</body></html>\n");
    http_response_send(res);

    return;
}
Beispiel #16
0
void
wiki_handle_http_request(HttpRequest *req)
{
    HttpResponse *res      = http_response_new(req);
    char         *page     = http_request_get_path_info(req);
    char         *command  = http_request_get_query_string(req);
    char         *wikitext = "";

    util_dehttpize(page); 	/* remove any encoding on the requested
				   page name.                           */

    if (!strcmp(page, "/"))
    {
        if (access("WikiHome", R_OK) != 0)
            wiki_redirect(res, "/WikiHome?create");
        page = "/WikiHome";
    }

    if (!strcmp(page, "/styles.css"))
    {
        /*  Return CSS page */
        http_response_set_content_type(res, "text/css");
        http_response_printf(res, "%s", CssData);
        http_response_send(res);
        exit(0);
    }

    if (!strcmp(page, "/favicon.ico"))
    {
        /*  Return favicon */
        http_response_set_content_type(res, "image/ico");
        http_response_set_data(res, FaviconData, FaviconDataLen);
        http_response_send(res);
        exit(0);
    }


    page = page + 1; 		/* skip slash */

    if (!strncmp(page, "api/", 4))
    {
        char *p;

        page += 4;
        for (p=page; *p != '\0'; p++)
            if (*p=='?') {
                *p ='\0';
                break;
            }

        wiki_handle_rest_call(req, res, page);
        exit(0);
    }

    /* A little safety. issue a malformed request for any paths,
     * There shouldn't need to be any..
     */
    if (strchr(page, '/'))
    {
        http_response_set_status(res, 404, "Not Found");
        http_response_printf(res, "<html><body>404 Not Found</body></html>\n");
        http_response_send(res);
        exit(0);
    }

    if (!strcmp(page, "Changes"))
    {
        wiki_show_changes_page(res);
    }
    else if (!strcmp(page, "ChangesRss"))
    {
        wiki_show_changes_page_rss(res);
    }
    else if (!strcmp(page, "Search"))
    {
        wiki_show_search_results_page(res, http_request_param_get(req, "expr"));
    }
    else if (!strcmp(page, "Create"))
    {
        if ( (wikitext = http_request_param_get(req, "title")) != NULL)
        {
            /* create page and redirect */
            wiki_redirect(res, http_request_param_get(req, "title"));
        }
        else
        {
            /* show create page form  */
            wiki_show_create_page(res);
        }
    }
    else
    {
        /* TODO: dont blindly write wikitext data to disk */
        if ( (wikitext = http_request_param_get(req, "wikitext")) != NULL)
        {
            file_write(page, wikitext);
        }

        if (access(page, R_OK) == 0) 	/* page exists */
        {
            wikitext = file_read(page);

            if (!strcmp(command, "edit"))
            {
                /* print edit page */
                wiki_show_edit_page(res, wikitext, page);
            }
            else
            {
                wiki_show_page(res, wikitext, page);
            }
        }
        else
        {
            if (!strcmp(command, "create"))
            {
                wiki_show_edit_page(res, NULL, page);
            }
            else
            {
                char buf[1024];
                snprintf(buf, 1024, "%s?create", page);
                wiki_redirect(res, buf);
            }
        }
    }

}