Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6188681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:16:43+00:00 2026-05-24T02:16:43+00:00

I am trying to use a CGI script to act as static provider for

  • 0

I am trying to use a CGI script to act as static provider for my application assets. Since I am relatively new to Perl, I don’t think my code is the best way to achieve what I want. If you have better code, let me know. Here’s my recent script

#!/usr/bin/perl
use lib "/usr/share/perl/5.10.1";
$asset_path = "/home/foo/public_html/assets/img/";
$uri_asset_path = "http://v2.foo.com/assets/img/";
use File::stat;
use Time::localtime;
$ENV{QUERY_STRING}=~ s/:/\//g;
$file = $asset_path . $ENV{QUERY_STRING};
unless (-e $file) {
print header('text/html','404 Not Found'); 
exit();
} 
$raw_last_modified = ctime(stat($file)->mtime);
$age = 30*24*60*60;
$date_day = substr $raw_last_modified, 0, 3;
$date_mon = substr $raw_last_modified, 4, 3;
$date_mon = substr $raw_last_modified, 4, 3;
$date_d = substr $raw_last_modified, 8, 2;
$date_time = substr $raw_last_modified, 11, 8;
$date_year = substr $raw_last_modified, 20, 4;
$last_modified = "$date_day, $date_d $date_mon $date_year $date_time GMT";
use CGI qw/:standard/;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $response = $ua->get("$uri_asset_path/$ENV{QUERY_STRING}");
unless ($response->is_success){
    print header('text/html','404 Not Found'); 
    exit();
}
if(defined($ENV{'HTTP_IF_MODIFIED_SINCE'})){
    if ($ENV{'HTTP_IF_MODIFIED_SINCE'} == $last_modified){
        print header('text/html','304 Not Modified');
        return();
    } 
}
print header(-type=>'image/png',-expires=>'+30d',-last_modified => $last_modified,-cache_control => 'max-age='.$age.', must-revalidate');
print $response->content;

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:16:43+00:00Added an answer on May 24, 2026 at 2:16 am

    It does not look like you have a specific question. I am assuming you are soliciting advice on how to write a CGI script properly.

    I do not see why there is any need to write a CGI script for this. Just configure your web server properly to add expires and Etag information.

    You should add use warnings; and use strict; to your script, run it in ‘taint’ mode, not use $ENV{QUERY_STRING} directly, but access variables provided using CGI.pm‘s param method etc.

    I do not understand why LWP::UserAgent makes an appearance, why you are issuing a GET.

    Perhaps you can explain why you are doing what you are doing and formulate a specific question in line with the Q&A nature of this site.

    In addition, you might want to read How (and how not) to Control Caches.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am relatively new to Perl.I have been trying to insert data to database
I just discovered wkhtmltopdf and I'm trying to use it in a perl CGI
(Context: I'm trying to monitor a long-running process from a Perl CGI script. It
I was trying out an elementary Perl/CGI script to keep track of visitors coming
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
Trying to use this code to connect the AD PrincipalContext context = new PrincipalContext(ContextType.Domain,
I'm trying to update some existing code that normally runs a Perl script which
So I'm trying to use the CGI module that comes with Python, on a
I'm trying to determine, within a Perl script on Linux, whether it's running in
So, I got a PERL script I am trying to run, it start like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.