1

I have a problem when handling the HTTPS response message. I want to get cookie's content from Set-Cookie fields in response header I received.
This is my function:

void Get_cookie(char *ck, char *message) {

const int COOKIE_SIZE = 102400;
int i, j, length = 0;
char temp[] = "Set-Cookie: ";
char cookie[COOKIE_SIZE];
char *start = NULL;
char *msg = NULL;
char *st = NULL;

msg = strstr(message, temp);                        // find set-cookie in message
if (msg == NULL){                                   // not found
    strcpy(ck, "null");                             // return null
    return;
}
start = (char *) malloc(COOKIE_SIZE*sizeof(char));
strcpy(start, msg);                                 // copy msg to start
do {
    st = strstr(start, temp);                       // find set-cookie in start
    if (st == NULL) break;                          // don't have anymore => break
    st += strlen(temp);                             // move st pointer to content of feild
    i = 0;
    while (st[i] != '\n') i++;                      // find the character endline '\n'
    for (j = 0; j < i; j++) {                       // copy content to cookie
        cookie[length+j] = st[j];
    }
    cookie[length+i] = ';';                         // add ;
    length += (i + 1);
    start += (strlen(temp) + i + 1);                // move start pointer to next feild
} while (st != NULL);
cookie[length] = '\0';
strcpy(ck, cookie);                 // return via ck
printf("Cookie: \n%s\n", cookie);

}

Where message is the response message I received. When I call the function, I received a result with characters ';' are at the head of each line (unexpected), like this:

Before resize terminal:

But the strange thing is, when I resize the terminal and run the program again, I received the result that all the character ';' are still at the head each line again. Like this

After resize terminal:

I don't know what worked wrong? My code or terminal. Help me please.

pah
  • 4,700
  • 6
  • 28
  • 37
thanhdx
  • 608
  • 4
  • 16

0 Answers0