?
Solved

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

Posted on 2016-11-27
6
Medium Priority
?
97 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
Technology Partners: 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

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
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.
Suggested Courses

762 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