I Can't set headers in Perl.
print "Expires: Thu, 08 May 2003 08:37:25 GMT\n\n";
print "Content-Type: text/html; charset=windows-1251\n\n";
print "Vary: Accept-Encoding\n\n";
First one works only. Then I have Content-Type: text/x-perl. What is wrong?
I'll assume you're using CGI to connect your web server to Perl. CGI uses a blank line to separate the headers from the response body. Since
print "Expires: Thu, 08 May 2003 08:37:25 GMT\n\n";
prints a blank line after the Expires: header, the remaining print statements are considered part of the body, not headers. You wanted:
print "Expires: Thu, 08 May 2003 08:37:25 GMT\n";
print "Content-Type: text/html; charset=windows-1251\n";
print "Vary: Accept-Encoding\n\n";
Related
I'm using Motion to manage a camera and it works perfectly.
I've configured mutt so it can send emails when an event occur on the camera.
When I send an email via mutt by myself it works every times.
But when it's motion that send the email, the "from:" field is not good and it doesn't send the email. Do you know why ? I've configured my ~/.muttrc file and it works when I do it without motion.
my ~/.muttrc :
# Name of expeditor
set realname = "Camera"
# Activate TLS if available on the server
set ssl_starttls=yes
# Always use SSL for connection to the server
set ssl_force_tls=yes
# Configuration of imap (Only if you want to be able to read emails on the Pi)
#set spoolfile=imaps://imap.gmail.com:993/
#set imap_user = "XXXXXXXX#gmail.com"
#set imap_pass = "PASSWORD"
# Configuration SMTP
set smtp_url = "smtp://[MY_EMAIL]#smtp.gmail.com:587/"
set smtp_pass = "[MY_PASSWD]"
set from = "[MY_EMAIL]"
set use_envelope_from=yes
# Local info, date
#set locale="fr_FR"
set date_format="%A %d %b %Y à %H:%M:%S (%Z)"
#set locale="iso-8859-15"
Normal "sent" file (when I do it without Motion) :
From john.smith#gmail.com Wed May 2 07:28:59 2018
Date: Wed, 2 May 2018 07:28:59 +0000
From: =?iso-8859-1?Q?Camera <john.smith#gmail.com>
To: john.smith#gmail.com
Subject: Test
Message-ID: <20180502072859.qhqyhfwcs5nw7quj#RPi>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: NeoMutt/20170113 (1.7.2)
Status: RO
Content-Length: 2
Lines: 1
Wrong "sent" file (when I do it with Motion) :
From motion#RPi Wed May 2 07:17:07 2018
Date: Wed, 2 May 2018 07:17:07 +0000
From: motion#RPi
To: john.smith#gmail.com
Subject: Picture
Message-ID: <20180502071707.nx7asct4m2hds6vt#RPi>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
User-Agent: NeoMutt/20170113 (1.7.2)
Status: RO
Content-Length: 39
Lines: 1
Can you help me ?
Thank you very much
A.
I'm using "mailsend v.1.19" to text a webmail client.
When I set mime-type, whether is with message body or one line message, I cannot find any trace of it into the raw email.
e,g:
mailsend -smtp local-mail.com -f sender#sender.local -t receiver#receiver.local -sub "Another Test Email With HTML body and mime type set from file body" -msg-body ~/justbody.html -starttls -user xxx -pass xxxx -mime-type "text/html"
I'm deliberately not setting content-type for testing purposes.
My SMPT ingestor will get the following raw stream with no signs of "text-html":
Received: from localhost (unknown [10.91.142.1])
by local-mail.dev.com (Postfix) with ESMTPS
for receiver#receiver.local; Wed, 31 Jan 2018 11:37:10 +0000 (UTC)
Subject: Another Test Email With HTML body and mime type set from file body
From: sender#sender.local
Date: Wed, 31 Jan 2018 11:37:10 +0000
To: receiver#receiver.local
X-Mailer: #(#) mailsend v1.19 (Unix)
X-Copyright: BSD. It is illegal to use this software for Spamming
Mime-version: 1.0
Content-Type: (null); charset=utf-8
What am I getting wrong?
I have tried using mailsend with both parameters -content-type and mime-type both placed before the message body in the command line.
The result is that the header Content-type is filled with mime-type value.
So when mime-type is set, content-type header contains its value, when both are set, content-type still takes the mime-type parameter value and ignores the content-type.
I believe this issue is related to mailsend clean in documentation what is the logic behind it.
I currently expand my test suite to increase the test coverage. I want to test my controller and the html output that it renders, but I found a problem in using delete methods. Let me explain it in an example.
I have a route:
$r->delete('/backups/:id')
->to('backup#delete_backup')
->name('backup_delete');
that points to the following function in the backup controller:
sub delete_backup {
my $self = shift;
my $id = $self->param('id');
if ( something ) {
$self->flash( msg => "Backup id $id deleted!" );
}
else{
$self->flash( msg => "Cannot delete, backup id $id not found!" );
}
$self->redirect_to($self->url_for('backup_index'));
}
where the method that handles the route backup_index just displays the $msg and shows few other irrelevant data.
Next, I want to test this method, so I write a test:
$t_logged_in->ua->max_redirects(3);
my $page = $t_logged_in->app->url_for( 'backup_delete', id => $backup_id );
$t_logged_in->delete_ok($page)
->status_isnt( 404, "Checking: 404 $page" )
->status_isnt( 500, "Checking: 500 $page" );
The test is passed. But now, I want to check if the text is correct on the web page that is shown after redirecting. So I do the following:
$t_logged_in->ua->max_redirects(3);
my $page = $t_logged_in->app->url_for( 'backup_delete', id => $backup_id );
$t_logged_in->delete_ok($page)
->status_isnt( 404, "Checking: 404 $page" )
->status_isnt( 500, "Checking: 500 $page" )
->content_unlike(qr/Cannot delete,/i)
->content_like(qr/deleted/i);
The test fails. It fails because the content is empty, so the matching is done as there were:
'' =~ /deleted/i;
'' !~ /Cannot delete,/i;
and this is of course false in both cases. Of course, in the browser, the redirects work perfectly and I see everything as designed in the test. I can change the method to POST or GET but I wanted to make the routing properly in the way an API would be designed.
Question: how to design the test such that the content can be matched after the redirect?
For those who want to dig deeper, I give links to Github.
Routing definition line 303
Controller Backup function delete_backup line 135
Functional Test of Backup Controller line 53
Sorry no one answered this yet. I used to try to keep a closer eye on stack overflow but I got lazy :-P. This is a very interesting question.
Standard redirects (301/302) use the same verb as the original request UNLESS the original request was a POST Interestingly, this behavior was not really intended. A redirect is supposed to use the same request method as the original, but the POST->GET redirect is so commonly intended that most browsers added this behavior even though it was against the original definition. However some people really wanted a POST to remain a POST and now it depended on the implementation. In fact this got so bad that HTTP added response statuses 307/308 which always redirects as the same http verb (ie POST -> POST), even thought that was already the official behavior of the old ones but that ship had sailed. This means that POST->GET, but DELETE->DELETE is the defacto behavior of the 301/302.
This can be demonstrated in the following one liner ($_ in these one-liners is the controller):
perl -Mojo -E 'del "/delete" => sub { $_->redirect_to("/") }; any "/" => { inline => q[I got a <%= $c->req->method %>] }; app->start' get -r -v -M DELETE /delete
DELETE /delete HTTP/1.1
Host: 127.0.0.1:62690
Accept-Encoding: gzip
User-Agent: Mojolicious (Perl)
Content-Length: 0
HTTP/1.1 302 Found
Date: Sun, 18 Sep 2016 03:25:58 GMT
Server: Mojolicious (Perl)
Location: /
Content-Length: 0
DELETE / HTTP/1.1
Content-Length: 0
User-Agent: Mojolicious (Perl)
Host: 127.0.0.1:62690
Accept-Encoding: gzip
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Content-Length: 5
Date: Sun, 18 Sep 2016 03:25:58 GMT
Server: Mojolicious (Perl)
I got a DELETE
You can see that since my / route handles all methods we get a response, however the request was a DELETE. I see that in your app, you redirect to another named route whose url also handles DELETE. This is a source of your confusion as you are actually ending up there in your tests. Your redirect is effectively DELETE /backups/<<id>> -> DELETE /backups, if I'm reading your code correctly.
Now as opposed to the people who wanted to keep POST as POST, what you want here is the opposite, you want to redirect from DELETE to GET because you are trying to show a response that is not the original resource. This is the defined behavior of the little-known 303 response in which any request method is redirected to a GET. Indeed this is what early browsers should have insisted on rather than breaking the 302.
In the following example I modify the response code to 303 explicitly.
$ perl -Mojo -E 'del "/delete" => sub { $_->res->code(303); $_->redirect_to("/") }; any "/" => { inline => q[I got a <%= $c->req->method %>] }; app->start' get -r -v -M DELETE /delete
DELETE /delete HTTP/1.1
User-Agent: Mojolicious (Perl)
Host: 127.0.0.1:62716
Accept-Encoding: gzip
Content-Length: 0
HTTP/1.1 303 See Other
Location: /
Content-Length: 0
Server: Mojolicious (Perl)
Date: Sun, 18 Sep 2016 03:27:19 GMT
DELETE / HTTP/1.1
User-Agent: Mojolicious (Perl)
Accept-Encoding: gzip
Content-Length: 0
Host: 127.0.0.1:62716
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Server: Mojolicious (Perl)
Content-Length: 5
Date: Sun, 18 Sep 2016 03:27:19 GMT
I got a DELETE
But whoops, that's still a DELETE! That's because there was a bug in Mojo::UserAgent so that it didn't handle 303s correctly. I'd suggest you still make the change to your code since browsers seem to handle 303s correctly. However since Mojo::UserAgent powers Test::Mojo, your tests can't test that behavior yet. Once fixed you will see it work correctly, as my example does in my local branch:
$ perl -Ilib -Mojo -E 'del "/delete" => sub { $_->res->code(303); $_->redirect_to("/") }; any "/" => { inline => q[I got a <%= $c->req->method %>] }; app->start' get -r -v -M DELETE /delete
DELETE /delete HTTP/1.1
Content-Length: 0
User-Agent: Mojolicious (Perl)
Host: 127.0.0.1:64924
Accept-Encoding: gzip
HTTP/1.1 303 See Other
Date: Sun, 18 Sep 2016 04:59:37 GMT
Location: /
Server: Mojolicious (Perl)
Content-Length: 0
GET / HTTP/1.1
Content-Length: 0
User-Agent: Mojolicious (Perl)
Host: 127.0.0.1:64924
Accept-Encoding: gzip
HTTP/1.1 200 OK
Date: Sun, 18 Sep 2016 04:59:37 GMT
Content-Type: text/html;charset=UTF-8
Server: Mojolicious (Perl)
Content-Length: 12
I got a GET
To see more about redirect response types see https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#3xx_Redirection
I am using this code to send Gmail from a perl script.
use strict;
use warnings;
use Email::Send::SMTP::Gmail;
my ($mail,$error)=Email::Send::SMTP::Gmail->new( -smtp=>'smtp.gmail.com',
-login=>'xxxxxxx#gmail.com',
-pass=>'xxxxxxx');
#print "session error: $error" unless ($email!=-1);
$mail->send(-to=>'zzzzzz#gmail.com', -subject=>"Hello! there $name", -body=>'Just testing it',
-attachments=>'full_path_to_file');
$mail->bye;
This code is inside an for loop and $name is the name of intended person.
When I tested it I got some meta-data in my mail
Date: Fri, 12 Dec 2014 12:44:37 +0530
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
This doesn't happen if I use 'Hello' as -subject . The $name variable is causing this . How can I remove the metadata from the mail ?
You could be facing Image Blocking in HTML Email.
use WWW::Curl::Easy;
$curl->setopt(CURLOPT_HEADER,1);
$curl->setopt(CURLOPT_RETURNTRANSFER,1);
$curl->setopt(CURLOPT_URL,"http://example.com/login.php");
$curl->setopt(CURLOPT_POSTFIELDS,"user=usertest&pass=passwdtest");
$curl->perform();
It will printout like this.
How do I get the output into a variable from perform function?
HTTP/1.1 302 Found Cache-Control:
no-cache, must-revalidate Expires:
Sat, 11 Jan 200 05:00:00 GMT
Location: ?cookiecheck=1
Content-type: text/html Date: Thu, 28
Apr 2011 09:15:57 GMT Server:
xxxx/0.1 Content-Length: 0
Connection: Keep-Alive Set-Cookie:
auth=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx;
expires=Sat, 27-Apr-2013 09:15:57 GMT;
path=/; domain=.example.com
I agree with PacoRG that you most likely should look into using a module from the LWP:: space. Since you have more specific needs I would recommend the LWP::UserAgent.
That said if you really need to get something which is being printed to instead be stored in a variable, we can play some games with deeper Perl magic.
# setopt method calls here
## the variable you want to store your data in
my $variable;
{
## open a "filehandle" to that variable
open my $output, '>', \$variable;
## then redirect STDOUT (where stuff goes when it is printed) to the filehandle $output
local *STDOUT = $output;
## when you do the perform action, the results should be stored in your variable
$curl->perform();
}
## since you redirected with a 'local' command, STDOUT is restored outside the block
## since $output was opened lexically (with my), its filehandle is closed when the block ends
# do stuff with $variable here
Perhaps WWW::Curl::Easy has a better way of doing this, since I don't know that module's commands I have provided you with a hack that will do what you need.
What are you trying to do? May be LWP::Simple is what you need...
Over 11 years later, I hope this helps:
my $response;
sub write_data($$$$) {
$response = shift;
return length($response);
}
# ... curl setup
$curl->setopt(CURLOPT_WRITEFUNCTION,\&write_data);
# ... curl perform
print "$response\n"