Solved

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

Posted on 2016-11-27
6
46 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 31

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 31

Expert Comment

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

Have a nice day,

best regards,

ZOPPO
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

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

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Statment to match two tables in c# 6 77
Convert image to byte array 8 189
Console based application in Linux 1 84
Want to delete all my personal data 13 137
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

910 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

24 Experts available now in Live!

Get 1:1 Help Now