Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-11-27
6
Medium Priority
?
112 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

650 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