get(buffer, size) and CGI

C

Chris

Hi, I'm using CGI with MS IIS and Visual Studio 2003.

My problem is when I'm trying to read post data, I use
cin.get(buffer, size), where buffer is a new char[size + 1] and size
is the atoi of getenv("CONTENT_LENGTH").

My problem is, it cuts off one character at the end. And if I use
cin.get(buffer, size + 1), it just sits there waiting for more data.

Do I have to roll my own solution, or use fread? Is there some way to
use cin.get work for me, without having to call it twice (the second
time calling cin.get(buffer[size-1])?
 
J

Josh Sebastian

Chris said:
Hi, I'm using CGI with MS IIS and Visual Studio 2003.

My problem is when I'm trying to read post data, I use
cin.get(buffer, size), where buffer is a new char[size + 1] and size
is the atoi of getenv("CONTENT_LENGTH").

My problem is, it cuts off one character at the end.

cin.get(foo, n) will extract up to n-1 characters, so that is the expected
behavior. (The idea is that if you allocate n characters, you just continue
passing n around instead of having to do arithmetic on it.)
And if I use
cin.get(buffer, size + 1), it just sits there waiting for more data.

Hmm... that doesn't sound right. Just tested with Apache (don't have IIS),
and it worked fine. If you really need to get that method working for some
reason, try posting to an IIS- (or at least CGI-) specific group.
Do I have to roll my own solution, or use fread? Is there some way to
use cin.get work for me, without having to call it twice (the second
time calling cin.get(buffer[size-1])?

Try using std::string.

#incude <iostream>
#include <string>

int main() {
using namespace std;

cout << "Content-type: text/plain\r\n";
cout << "\r\n";

string post_data;
getline(cin, post_data);

// parse post_data, etc

return 0;
}

You don't even need to worry about Content-length.
 
C

Chris

Chris said:
Hi, I'm using CGI with MS IIS and Visual Studio 2003.
My problem is when I'm trying to read post data, I use
cin.get(buffer, size), where buffer is a new char[size + 1] and size
is the atoi of getenv("CONTENT_LENGTH").
My problem is, it cuts off one character at the end.

cin.get(foo, n) will extract up to n-1 characters, so that is the expected
behavior. (The idea is that if you allocate n characters, you just continue
passing n around instead of having to do arithmetic on it.)
And if I use
cin.get(buffer, size + 1), it just sits there waiting for more data.

Hmm... that doesn't sound right. Just tested with Apache (don't have IIS),
and it worked fine. If you really need to get that method working for some
reason, try posting to an IIS- (or at least CGI-) specific group.
Do I have to roll my own solution, or use fread? Is there some way to
use cin.get work for me, without having to call it twice (the second
time calling cin.get(buffer[size-1])?

Try using std::string.

#incude <iostream>
#include <string>

int main() {
using namespace std;

cout << "Content-type: text/plain\r\n";
cout << "\r\n";

string post_data;
getline(cin, post_data);

// parse post_data, etc

return 0;
}

You don't even need to worry about Content-length.

Well, I will try posting to an IIS group, but I suspected it was a
problem with my C++ compiler or my C++ code. The problem with using
getline is it doesn't fit the CGI standard, and when you get into
using multipart data, it could contain a text file, which would have
newlines in it already.
 
C

Chris

Chris said:
Hi, I'm using CGI with MS IIS and Visual Studio 2003.
My problem is when I'm trying to read post data, I use
cin.get(buffer, size), where buffer is a new char[size + 1] and size
is the atoi of getenv("CONTENT_LENGTH").
My problem is, it cuts off one character at the end.

cin.get(foo, n) will extract up to n-1 characters, so that is the expected
behavior. (The idea is that if you allocate n characters, you just continue
passing n around instead of having to do arithmetic on it.)
And if I use
cin.get(buffer, size + 1), it just sits there waiting for more data.

Hmm... that doesn't sound right. Just tested with Apache (don't have IIS),
and it worked fine. If you really need to get that method working for some
reason, try posting to an IIS- (or at least CGI-) specific group.
Do I have to roll my own solution, or use fread? Is there some way to
use cin.get work for me, without having to call it twice (the second
time calling cin.get(buffer[size-1])?

Try using std::string.

#incude <iostream>
#include <string>

int main() {
using namespace std;

cout << "Content-type: text/plain\r\n";
cout << "\r\n";

string post_data;
getline(cin, post_data);

// parse post_data, etc

return 0;
}

You don't even need to worry about Content-length.

I forgot to add, how would I go about viewing the state of cin (kinda
like extended cin.peek() and such)? You know, what it's got left in
it's buffer? Looking at the MS library source, it looks like cin.get
just keeps reading from cin.rdbuf()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,796
Messages
2,569,645
Members
45,366
Latest member
GayT017679

Latest Threads

Top