Solved

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

Posted on 2016-11-27
6
88 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

696 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