Skip to content

Instantly share code, notes, and snippets.

@Lycolia
Last active February 18, 2021 12:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lycolia/08374b5a246bafae3754f3fa036e3af0 to your computer and use it in GitHub Desktop.
Save Lycolia/08374b5a246bafae3754f3fa036e3af0 to your computer and use it in GitHub Desktop.
C-lang-CGI-example
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test.cgi [L]
</IfModule>
#include <stdio.h>
// getenv
#include <stdlib.h>
// strcmp
#include <string.h>
/*
get path for after hostname
*/
char* getPath(void) {
char *path = getenv("REQUEST_URI");
return path;
}
int main(void) {
// put http header
printf("Content-type: text/plain\n\n");
char *path = getPath();
// show current path
printf("Current path: %s\n", path);
// routing, required .htaccess to serve on apache
if (strcmp(path, "/foo") == 0) {
printf("hoge");
} else if (strcmp(path, "/bar") == 0) {
printf("piyo");
} else {
printf("fuga");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment