Help with splitting const char?

Hi, I am having some trouble with getting this working. I am working on some base code that I have. And I want to split the buffer and replace some things. I have tried countless other things, none have really worked.

I do not want to use boost or any thing like that, I just need a short piece of code to help me. Please do not ask me to use boost, I do not want to.

Here is what I am trying to achieve:

I have used Detours 2.1 and some online resources to get a working hook for Send. I am capturing the HTTP headers and I want to find and edit the Accept-Encoding line.

From:
1
2
3
GET / HTTP/1.1
[removed to shorten the post]
Accept-Encoding: gzip,deflate

To:
1
2
3
GET / HTTP/1.1
[removed to shorten the post]
Accept-Encoding: identity

Here is all that I have so far in my hooked Send:
1
2
3
4
5
6
7
8
9
10
11
12
13
int WINAPI Mine_Send( SOCKET s, const char *buf, int len, int flags )
{
	//MessageBox(NULL,buf,NULL,NULL);
	if (strncmp(buf,"POST",lstrlen("POST"))==0)
	{
		//this was just a test
	}
		ofstream sendlog;
		sendlog.open ("C:\\Users\\[User]\\Desktop\\buf.txt",ios::app);
		sendlog <<"Socket: " << s <<endl << "Buffer: " << buf <<endl;
		sendlog.close();
    return Real_Send( s, buf, len, flags ); 
}

I would really appreciate it if someone could help me out. I am still learning and would like to take the time out to mention that a short snippet would be the best for me.

Thanks.
You mean that you get the "From" as the argument in buf and you want to pass the edited version to Real_Send ?
I suggest that you use strings. A combination of find(), substr() and operator + should be sufficient. see http://www.cplusplus.com/reference/string/string/ for how to use them. What code you need depends on exact requirements. If the input will always end with what you posted, it could be as simple as
1
2
std::string input( buf );
std::string output = input.substr(0, input.length()-12) + "identity";

Then pass output.c_str() and output.length() to Real_Send.
No, the order of the header options will be different most of the time. Do you happen to know how to get each line (so many in between each \n)? I could just use that and then use strncmp to check if it starts with "Accept-Encoding:". If it does, I can just replace the line. Thanks.
If you use c functions, you'll need to deal with reallocation yourself..

Here's a bit of code to process every line:
1
2
3
4
5
6
7
8
bool newline = true;
for( const char * ptr = buf; *ptr != 0; ptr++ ){
   if(newline){
      //do whatever you like
      newline = false;
   }
   if( *ptr == '\n' ) newline = true;
}

Note that if since buf is const char*, you'll need to make a copy. If you don't know how much exactly the length will change, it's a bit tricky..

Will there be only one line like this?
If yes, try
1
2
3
4
std::string input = buf;
int pos = input.find("Accept-Encoding:");
int end_of_that_line = input.find('\n', pos);
std::string output = input.substr(0, pos) + "Accept-Encoding: identity" + input.substr(end_of_that_line);
Do test this before using it though, as I haven't.

Also, can it be that "Accept-Encoding:" would occur somewhere in the middle of a line and would not need to be replaced? If yes, check that pos == 0 || input[pos-1] == '\n'
Last edited on
Thanks, I'll try this out when I get home. It looks perfect for my scenario.
Topic archived. No new replies allowed.