Parse HTTP Post Response Response in C ++

I am writing a bot puzzle, an http server that, when hit, displays a default page with a text area to write code similar to http://codepad.org/ . When I print the following program.

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

I get the 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 to analyze information from the code key. I need to write this program to a temporary file and then do compilation / execution.

+6
source share
1 answer

First you need to decode the data. You can use this link .

All spaces are replaced by + , and all numbers after % are special - 2-digit hexadecimal encoded numbers - special characters with URL encoding (for example, + } , etc.).).

For example, you code will be translated into:

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

Where \r\n is CRLF, So from this you finally get:

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

which is exactly your code. Then you can write it to your temporary file and try to compile it.


Some things that come to my mind for a better application, such as:

  • make it multithreaded - you can process more than one such request at a time
  • add several queues for the received data - do not lose the data that comes in while your program processes the current request (queue queue)
  • Of course, sync threads and be careful with this
  • I think you will need IPC (Inter-Process Communication) - to communicate with your compiler process and to get errors, it gives you (unless you have a special API provided for your compiler)

Of course, these are just some of the tips that come to me. This would be a great exercise for any developer - IPC + multithreading + network programming + http! Excellent:)

Good luck.

+1
source

Source: https://habr.com/ru/post/894519/


All Articles