I created a while loop to read the response from the server I am sending. I am looking to strip out the http headers in the response. I'm a bit new to the world of c, so any guidance would help me learn!
Here's my code to read the response from the server:
#define MAXDATASIZE 1024
char buf[MAXDATASIZE];
FILE* pFile;
int nbytes_total = 0;
pFile = fopen("output","wb");
int loopcount = 0;
while ((nbytes_total = read(sockfd, buf, MAXDATASIZE)) > 0) {
fprintf(stderr, "debug: after a read\n");
printf( "LoopCount! (\"%i\") \n",loopcount);
if (strstr(buf, "404 File not found") != NULL) {
writeToOutPutFile("FILENOTFOUND");
}
if (pFile){
fwrite(buf, nbytes_total, 1, pFile);
puts("Wrote to file!");
}
write(STDOUT_FILENO, buf, nbytes_total);
printf( "LoopCount! (\"%i\") ****************\n",loopcount);
loopcount++;
}
and this is what the output looks like:
HTTP/1.0 200 OK
Server: SimpleHTTP/0.6 Python/3.6.2
Date: Mon, 14 Sep 2020 23:30:53 GMT
Content-type: text/html
Content-Length: 3566
Last-Modified: Wed, 10 May 2017 02:49:48 GMT
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Hello AWS Web World!</title>
<link rel="stylesheet" href="styles/styles.css" type="text/css" media="screen">
</head>
<body>
<div id="content" class="container">
<div style="height: 10px;"> </div>
<h3>AWS Java Web Project</h3>
<p>
Welcome to your new AWS Java Web Project! This project template gives you
basic a starting point for creating a web application on AWS.
</p>
<h4>Contents of this Project</h4>
<p>
This page is rendered from <b>WebContent/index.html</b> - a simple static
HTML page. Any static content in the WebContent folder will be accessible
from the equivalent path in your web application (for example,
https://your-app-93s2kypa5y.elasticbeanstalk.com/index.html).
</p>
<p>
Any <a href="https://jsp.java.net/">JSP</a> files in the WebContent folder
will be processed dynamically to generate HTML pages. For example, the
<a href="index.jsp">index.jsp</a> file renders a simple page that displays
a list of any <a href="http://aws.amazon.com/s3/">Amazon S3</a> Buckets,
<a href="http://aws.amazon.com/dynamodb/">Amazon DynamoDB</a> Tables,
and <a href="http://aws.amazon.com/ec2/">Amazon EC2</a> Instances currently
associated with your AWS account.
</p>
<h4>Deploying this Project Locally</h4>
<p>
If you have a local installation of <a href="http://tomcat.apache.org/">
Apache Tomcat</a>, you can deploy this project to it for local testing
using the standard Eclipse Web Tools Platform integration with Tomcat.
</p>
<ul>
<li>Right/control click on this project in the Project Explorer</li>
<li>Choose Run As, then Run on Server</li>
<li>Select the option to manually define a new server</li>
<li>Expand the Apache folder, then select an appropriate version of Tomcat</li>
<li>Point Eclipse at your local Tomcat installation and click Finish</li>
</ul>
<p>
This page will open up, now served by your local Tomcat installation.
</p>
<h4>Deploying the Project to AWS Elastic Beanstalk</h4>
<p>
<a href="http://aws.amazon.com/elasticbeanstalk/">AWS Elastic Beanstalk</a>
is the quickest and easiest way to deploy an AWS Java Web Project to the
cloud. To deploy this project to AWS Elastic Beanstalk:
</p>
<ul>
<li>Right/control click on this project in the Project Explorer</li>
<li>Choose run As, then Run on Server</li>
<li>Select the option to manually define a new server</li>
<li>Expand the Amazon Web Services folder, then select an appropriate version of Tomcat</li>
<li>Select the AWS region where your application will be hosted</li>
<li>Provide a name for your application and environment and click Finish</li>
</ul>
<p>
Your project will be uploaded to AWS Elastic Beanstalk, which will
provision infrastructure to host your application and deploy your code
to it. When it's finished, you will again see this page open up, now hosted
in the cloud!
</p>
<p>
For more information about working with AWS Elastic Beanstalk, refer to the
<a href="http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Java.html">
AWS Elastic Beanstalk Developer Guide</a>.
</p>
</div>
</body>
</html>
What I tried doing is looping through the buffer until the end of header ("\r\n\r\n") but I get some gibberish on the top of the file when doing so:
if (found == 0){
for (int i = 0; i < strlen(buf); i++){
if (buf[i] == '\r'){
if (buf[i+1] == '\n'){
}
if (buf[i-2] == '\r'){
htmlIndex=i+2;
found = 1;
if (pFile){
fwrite(&buf[htmlIndex], nbytes_total, 1, pFile);
puts("Wrote to file!");
}
}
}
}
} else {
if (pFile){
fwrite(buf, nbytes_total, 1, pFile);
puts("Wrote to file!");
}
}