Last active
February 18, 2021 12:23
-
-
Save Lycolia/08374b5a246bafae3754f3fa036e3af0 to your computer and use it in GitHub Desktop.
C-lang-CGI-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
RewriteBase / | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule . /test.cgi [L] | |
</IfModule> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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