Solved

Using popen() and gunzip() to open file in HTTPServer

Posted on 2016-11-27
6
15 Views
Last Modified: 2016-11-28
My program should simulate a simple httpServer replying to clients requests. It should answer both .html and .html.gz requests. My problem is when I receive a .gz request. Currently execute_script is handling that request and it unzips the file but I can't send it back to the client. What's the problem with my code?

    void execute_script(int socket)
    {
   
          FILE *gp;
          char temp[SIZE_BUF];
          
          // Searchs for page in directory htdocs
          sprintf(buf_tmp,"htdocs/%s",req_buf);
          sprintf(temp, "gunzip -k %s", buf_tmp);

          // Verifies if file exists
          if((gp=popen(temp,"r"))==NULL) {
                // Page not found, send error to client
                printf("send_page: page %s not found, alerting client\n",buf_tmp);
                not_found(socket);
          }
          else {
                // Page found, send to client
          
                // First send HTTP header back to client
                send_header(socket);
   
                printf("send_page: sending page %s to client\n",buf_tmp);
                while(fgets(buf_tmp,SIZE_BUF,gp)!=NULL)
                      send(socket,buf_tmp,strlen(buf_tmp),0);
                
                // Close file
                pclose(gp);
          }
   
   
          
          return;
    }
0
Comment
Question by:José Santos
  • 3
  • 3
6 Comments
 
LVL 30

Accepted Solution

by:
Zoppo earned 500 total points
ID: 41903696
Hi José Santos,

I think this only can work if the gunzip writes the output to stdout so you can retrieve the result via the pipe. To do so I think you need to add -c or --stdout as argument to the gunzip call.

Hope this helps,

ZOPPO
1
 

Author Closing Comment

by:José Santos
ID: 41903991
Thank you ZOPPO, your solution solved my problem and the server is now working!
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 41903996
You're welcome, I'm glad I could help.

Have a nice day,

best regards,

ZOPPO
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:José Santos
ID: 41903997
Can you just tell me what's the difference between -c and --stdout?
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 41904003
There's no difference, it is very common for Linux-tool to offer some or all options in two forms, a short-form with a single character prefixed by one - and a self-explanatory long-form prefixed by a double --.
1
 

Author Comment

by:José Santos
ID: 41904007
Thank you again, I had no idea it worked that way.

Have a nice day,

José Santos
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now