I'm trying to put together a http response header. It seems to work, except for that I don't get the data string added correctly!
I wouldn't be surprised if my problems have something to do with my still existing confusion over arrays and pointers in C since I'm still a newbie, but anyhow, here's the code:
I get this printout when I run this program:
Webbuflen is now: 123
And webbuf is:
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2008 09:14:59 GMT
Connection: close
Content-Type: text/html
Content-Length: 93
But I would like to get:
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2008 09:14:59 GMT
Connection: close
Content-Type: text/html
Content-Length: 93
<html><head><ti tle>Testing</title></head><body>Test ,test,test, testing, test.</body></html>
Any help or pointers in the right direction is greatly appreciated!
Cheers!
I wouldn't be surprised if my problems have something to do with my still existing confusion over arrays and pointers in C since I'm still a newbie, but anyhow, here's the code:
Code:
#include <time.h>
#include <stdio.h>
#define WEBBUF_SIZE 32768
int webbuflen;
char webbuf[WEBBUF_SIZE];
static char OKheader[] = "HTTP/1.1 200 OK\r\n";
static char texthtmlheader[] = "Connection: close\r\nContent-Type: text/html\r\n";
static char contentLenheader[] = "Content-Length: ";
static char data[] = "<html><head><title>Testing</title></head><body>Test,test,test, testing, test.</body></html>\n";
int main()
{
http_response();
return 0;
}
int getTimeStamp(unsigned char* timestring)
{
struct tm *ptr;
time_t tm;
tm = time(NULL);
ptr = localtime(&tm);
strftime(timestring ,100 , "Date: %a, %d %b %Y %H:%M:%S GMT\r\n",ptr);
return 0;
}
int http_response()
{
char tmp[10];
char datestring[60];
char header[1048];
//Get content-length (length of data) as a string
sprintf(tmp,"%d%s",(strlen(data)+1),"\r\n\r\n");
strcat(contentLenheader,tmp);
getTimeStamp(datestring);
//write everything to header
sprintf(header, "%s%s%s%s", OKheader,datestring, texthtmlheader,contentLenheader);
//Appending data to header
strcat(header,data);
//Adding header to webbuf
webbuflen = sprintf(webbuf, "%s",header);
printf("\nWebbuflen is now: %d",webbuflen);
printf("\nAnd webbuf is: \n\n%s",webbuf);
return 0;
}
Webbuflen is now: 123
And webbuf is:
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2008 09:14:59 GMT
Connection: close
Content-Type: text/html
Content-Length: 93
But I would like to get:
HTTP/1.1 200 OK
Date: Fri, 22 Feb 2008 09:14:59 GMT
Connection: close
Content-Type: text/html
Content-Length: 93
<html><head><ti tle>Testing</title></head><body>Test ,test,test, testing, test.</body></html>
Any help or pointers in the right direction is greatly appreciated!
Cheers!
Comment