summaryrefslogtreecommitdiffhomepage
path: root/midiplay.cpp
blob: 75f33daa85d7cfe895a8739b830402e2e3991013 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <string>

#include <fcgiapp.h>

#include <fmt/format.h>

std::string getPostData(FCGX_Request& request)
{
  std::string result;
  std::string contentLengthString(FCGX_GetParam("CONTENT_LENGTH", request.envp));
  int contentLength = std::stoul(contentLengthString);

  if (contentLength < 1) {
    return "Bad content length";
  } else {
    result.resize(contentLength);

    unsigned int status = FCGX_GetStr(result.data(), result.size(), request.in);
    if (status != result.size()) {
      return fmt::format("Read error: {}/{}", status, result);
    }

    return result;
  }
}

int main(int argc, char* argv[]) {

	int result = FCGX_Init();
	if (result != 0) {
		return 1; // error on init
	}

	FCGX_Request request;

	if (FCGX_InitRequest(&request, 0, 0) != 0) {
		return 1; // error on init
	}

	while (FCGX_Accept_r(&request) == 0) {
          std::string query = FCGX_GetParam("QUERY_STRING", request.envp);
          
          std::string method = FCGX_GetParam("REQUEST_METHOD", request.envp);

          if (method == "POST") {
            FCGX_PutS("Content-Type: text/xml\r\n\r\n", request.out);

            std::string data = getPostData(request);
            if (data == "<data><value1>3</value1></data>")
              FCGX_PutS("<data><value1>4</value1></data>", request.out);
          } else {
            FCGX_PutS("Content-Type: text/text\r\n\r\n", request.out);
            FCGX_PutS("Bad request method: POST expected", request.out);
          }

	}

	return 0;
}