来自fastcgi请求的输入流中缺少字符

时间:2010-11-25 21:31:42

标签: c apache fastcgi

我正在尝试使用FastCGI(和restcgi)开发简单的RESTful api。当我尝试实现POST方法时,我注意到输入流(表示请求体)是错误的。我做了一点测试,看起来当我尝试读取流时,只收到其他所有角色。

发送的身体:name=john&surname=smith 收到:aejh&unm=mt

我尝试过更多的客户只是为了确保客户端不会弄乱数据。 我的代码是:

int main(int argc, char* argv[]) {
  // FastCGI initialization.
  FCGX_Init();
  FCGX_Request request;
  FCGX_InitRequest(&request, 0, 0); 

  while (FCGX_Accept_r(&request) >= 0) {
    // FastCGI request setup.
    fcgi_streambuf fisbuf(request.in);
    std::istream is(&fisbuf);
    fcgi_streambuf fosbuf(request.out);
    std::ostream os(&fosbuf);

    std::string str;
    is >> str;
    std::cerr << str;  // this way I can see it in apache error log

    // restcgi code here
  }   

  return 0;
}

我正在使用带有apache的fast_cgi模块(不确定这是否有所不同)。

知道我做错了什么吗?

4 个答案:

答案 0 :(得分:2)

我在未经修改的Debian安装中遇到了这个问题。

如果我向fcgi_streambuf构造函数提供缓冲区,我发现问题就消失了:

const size_t LEN = ... // whatever, it doesn't have to be big.
vector<char> v (LEN);
fcgi_streambuf buf (request.in, &v[0], v.size());
iostream in (&buf);
string s;
getline(in, s); // s now holds the correct data.

答案 1 :(得分:2)

问题在于fcgio.cpp

fcgi_steambuf类是使用char_type定义的,但int underflow()方法将其返回值向下转换为(unsigned char),它应该转换为(char_type)

答案 2 :(得分:1)

在任何地方找不到答案(甚至不是FastCGI邮件列表)后,我抛弃了原来的fastcgi库并尝试使用fastcgi ++库。问题消失了。还有其他好处--c ++,更多功能,更易于使用。

答案 3 :(得分:1)

使用is.read()而不是is >> ...

来自restcgi文档的示例:

clen = strtol(clenstr, &clenstr, 10);
if (*clenstr)
{
    cerr << "can't parse \"CONTENT_LENGTH="
          << FCGX_GetParam("CONTENT_LENGTH", request->envp)
          << "\"\n";
    clen = STDIN_MAX;
}

// *always* put a cap on the amount of data that will be read
if (clen > STDIN_MAX) clen = STDIN_MAX;

*content = new char[clen];

is.read(*content, clen);
clen = is.gcount();
相关问题