#include #include #include // When compiled into a .cgi executable, this runs any query as a command // on the server, allowing you to download remotely and divide the file into // segments. // The default command.cgi outputs nothing but the executable's stdout. // The keepalive command2.cgi outputs something to keep the connection alive. // Assume it's being invoked from the command line, not a browser. int main(int argc, char *argv[]) { // Debugging only // printf("Content-type:text/html\n\n"); // printf("Content-type:application/octet-stream\n\n"); int i; char *query = getenv("QUERY_STRING"); char *the_command = argv[0]; #define SILENT_COMMAND "command.cgi" #define KEEPALIVE_COMMAND "command2.cgi" int is_keepalive = (int)(strstr(the_command, KEEPALIVE_COMMAND)); if(query && strlen(query)) { char *query2 = strdup(query); char *ptr = strchr(query, '%'); char *ptr2 = query; char *ptr3 = query2; unsigned char character; // Convert % codes to characters while(ptr) { // Copy leading good text while(ptr2 < ptr) *ptr3++ = *ptr2++; // Convert hex to character ptr++; character = ' '; if(*ptr) { if(isdigit(*ptr)) character = (*ptr - '0') * 0x10; else character = (tolower(*ptr) - 'a' + 10) * 0x10; ptr++; if(*ptr) { if(isdigit(*ptr)) character += (*ptr - '0'); else character += (tolower(*ptr) - 'a' + 10); ptr++; } } *ptr3++ = character; ptr2 = ptr; ptr = strchr(ptr, '%'); } while(*ptr2) *ptr3++ = *ptr2++; *ptr3++ = 0; // printf("query = %s\n", query2); // printf("result = "); // fflush(stdout); FILE *file = popen(query2, "r"); if(is_keepalive) { printf("Content-type:text/html\n\n"); printf("Keepalive mode.\n"); while(!feof(file)) { fd_set fds; struct timeval tv; int result; FD_ZERO(&fds); FD_SET(fileno(file), &fds); tv.tv_sec = 1; tv.tv_usec = 0; result = select(fileno(file) + 1, &fds, 0, 0, &tv); // Timed out if(!result) { printf(".\n"); fflush(stdout); } else if(result == -1) { // Done break; } else { character = fgetc(file); } } } else { printf("Content-type:application/octet-stream\n\n"); while(!feof(file)) { character = fgetc(file); if(feof(file)) break; fputc(character, stdout); } } pclose(file); if(is_keepalive) { // Client closes as soon as it gets Done. so must wait for pclose printf("D"); fflush(stdout); } free(query2); } else { printf("Content-type:text/html\n\n"); printf("No query received you MOR*ON!\n"); } fflush(stdout); return 0; }