0

i got the delimiter "\r\n\r\n" in the substring, and strstr is returning null

Here is the code :

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int ac, char **av) {
  char *ptr;

  ptr = strstr(av[1], "\r\n\r\n");
  printf("ptr = %s\n", ptr);
  return 0;
}

I launch the code with this :

./a.out "POST /cgi-bin/process.cgi HTTP/1.1\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\nHost: www.tutorialspoint.com\nContent-Type: text/xml; charset=utf-8\nContent-Length: length\nAccept-Language: en-us\nAccept-Encoding: gzip, deflate\nConnection: Keep-Alive\r\n\r\n<?xml version='1.0' encoding='utf-8'?>"

And ptr is equal to (null), why ?

Zahreddine Laidi
  • 560
  • 1
  • 7
  • 20

3 Answers3

2

The string you passed to the code contains the eight character sequence \, r, \, n, \, r, \, n.

The string literal "\r\n\r\n" produces the four character sequence , , , .


To produce a string that would match the argument, use the following string literal:

"\\r\\n\\r\\n"

But I think it's more likely you want to providing a proper HTTP request.

Depending on which echo you use, echo or echo -e might produce the desired string (plus a trailing line feed):

$ echo -e 'a\r\nb\r\n' | od -c
0000000   a  \r  \n   b  \r  \n  \n
0000007

printf can reliably produce exactly the string you want, though you have to escape % symbols by duplicating them.

$ printf 'a\r\n%%\r\n' | od -c
0000000   a  \r  \n   %  \r  \n
0000006

Example usage:

./a.out "$( printf 'POST ...\r\n\r\n...' )"
ikegami
  • 367,544
  • 15
  • 269
  • 518
0

In your case you have to use strstr(av[1], "\\r\\n\\r\\n")

RoiS
  • 31
  • 2
  • That is not what is intented. The problem is how to provide a string via command line that contains cr and lf. – Gerhardh May 16 '21 at 17:46
0

you can also use bash $'string' quoting:

$ ./a.out $'POST /cgi-bin/process.cgi HTTP/1.1\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; Windows NT)\nHost: www.tutorialspoint.com\nContent-Type: text/xml; charset=utf-8\nContent-Length: length\nAccept-Language: en-us\nAccept-Encoding: gzip, deflate\nConnection: Keep-Alive\r\n\r\n<?xml version=\'1.0\' encoding=\'utf-8\'?>'