#include #include #include /* Trying to duplicate the behavior: */ int main(void) { //get a pointer to the Query String passed in by IIS char *data1 = getenv("QUERY_STRING"); //character pointer to point to query data char *old_string = data1; //second pointer to first char in data string //allocate a clean string of equal size that will hold the URL-Decoded version char *data = calloc(strlen(data1), sizeof(char)); char *new_string = data; //put the pointer at the beginning //allocate a char buffer for hex decoding char val[2]; char c; //we need to manually URL-Decode the input string int i; for (i=0; *old_string != '\0'; old_string++) { //starting with a pointer at the beginning of the string //look at each character until we encouter the NULL at the end of the data string //increment the pointer by 1 memory unit each iteration if( *old_string == '%'){ //if we encounter a % sign, the next 2 chars represent //the hexidecimal value for a character //get the next 2 chars and store them in val[0]val[1] old_string++; val[0]=*old_string; old_string++; val[1]=*old_string; //now convert to decimal //convert to lower case and check A-F use the corresponding value switch( tolower(val[1]) ){ case 'a': i=10; break; case 'b': i=11; break; case 'c': i=12; break; case 'd': i=13; break; case 'e': i=14; break; case 'f': i=15; break; //if it is not A-F, it is a number 0-9 so just use the numeric //representation default: i=atoi( &val[1] ); break; } c=val[0]; //now convert to ASCII i+=16*(atoi( &c )); //now that we have the ASCII URL-Decoded value //copy it into the new string *new_string=i; new_string++; } else if( *old_string == '+'){ //plus'es become spaces *new_string=' '; new_string++; } else { //anything else is non-special and gets copied as is. *new_string = *old_string; new_string++; } } //Print HTTP header (required by RFC 2616, Hypertext Transfer Protocol - HTTP/1.1) printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10); if(data == NULL) { //no query string found printf("No query string."); return(-1); //nothing to do } else { int i = 0; int nEq = 0; int nAmp = 0; int e1Pos; int ampPos; char * e1; char * e2; //SPLIT THE QUERY STRING: // make sure there are exactly 2 ='s and 1 & char while (i < strlen(data)) { char c = data[i]; if (c == '=') { if (nEq == 0) { e1Pos = i; } nEq++; } if (c == '&') { nAmp++; ampPos = i; } i++; } if (nAmp != 1 || nEq != 2) { printf("Invalid number of query arguments.\n"); return(-1); } //note the locations of both = signs in the data string e1 = strchr(data, '='); e2 = strrchr(data, '='); //the length of arg1 is the number of chars between the first = sign //and the & sign, - 1 because we start counting at 0. int arg1Length = ampPos - e1Pos - 1; //allocate a string to hold the content path char *contentPath = calloc(arg1Length + 1, sizeof(char)); //the filename is the remainder of the string starting with this pointer char *fileName = e2 + 1; //copy the first arg from the data string into the new contentPath variable strncpy(contentPath, e1+1, arg1Length); //query string successfully parsed. Launch a process to generate the PDF int statusCode; //build input filename = #contentpath##filename#.html //since we don't know the max length of contentPath + fileName + .html at compile time //we need to count the length of the string and dynamically allocate a char array buffer //to hold this //adding 6 extra places - 1 for the ., 4 for "html" and one for the null char at the end of the string int strLen = strlen(contentPath) + strlen(fileName) + 6; //dynamically allocate clean (byte-zero'ed) strLen x size of character bytes of memory char * outPath = calloc(strLen + 1, sizeof(char)); char * inPath = calloc(strLen + 1, sizeof(char)); //copy the char arrays into the new memory buffers sprintf(outPath,"%s%s.pdf", contentPath, fileName); sprintf(inPath, "%s%s.html", contentPath, fileName); //build a command string and ask the OS to launch a shell & run it char * cmd = calloc(80 + strlen(outPath) + strlen(inPath), sizeof(char)); sprintf(cmd, "c:\\progra~1\\HTMLDOC\\htmldoc.exe -f %s --compression=9 --jpeg --webpage %s", outPath, inPath); printf("Running: %s
\n", cmd); //tells the operating system to execute command cmd and prints the return status to the browser printf("Process returned status: %i", system(cmd)); //free dynamic memory free(contentPath); free(outPath); free(inPath); free(cmd); free(data); //exit with no error exit(0); } }