Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP Post Response parsing in C++

Tags:

c++

http

I am writing puzzle bot, http server which upon hitting , renders a default page with text area to write code similar to http://codepad.org/. When I type in following program.

#include <stdio.h>
int main( int argc, char **argv) {
    return 0;
}

I get following response from HTTP POST.

code : %23include+%3Cstdio.h%3E%0D%0Aint+main%28+int+argc%2C+char+**argv%29+%7B%0D%0A++++return+0%3B%0D%0A%7D
lang : C

How Do I parse the information from the key code. I need to write this program in a temporary file and then compile/run.

like image 648
Avinash Avatar asked Aug 08 '11 05:08

Avinash


1 Answers

You need to decode the data, first. You could use this reference .

All spaces are replaces with the sign +, and all numbers after % are special - 2 digit hex encoded numbers - URL encoded special symbols (like +, ,, }, etc.).

For example, you code will be translated to :

#include <stdio.h>\r\nint main( int argc, char **argv) {\r\n    return 0;\r\n}

Where \r\n are CRLF, So, from this, you'll finally get:

#include <stdio.h>
int main( int argc, char **argv) {
    return 0;
}

which is exactly your code. Then you could write it to your temp file and try to compile it.


Some things, that come to my mind for better application like this:

  • make it multithreading - you'll be able to handle more than one such request at the same time
  • add some queues for the received data - don't lose the data that comes, while your program is processing the current request (kinda queue)
  • of course, synchronize the threads and be careful with that
  • I think you're going to need IPC (Inter-Process Communication) - to communicate with your compiler's process and extract the errors, it gives you (unless you have some special API, provided for your compiler)

Of course, these are just some advice, that come to my mind. That would be great exercise for any developer - IPC + multithreading + network programming + http! Great :)

Good luck

like image 114
Kiril Kirov Avatar answered Oct 20 '22 18:10

Kiril Kirov