vars not working after rewrite_by_lua_block - if-statement

see the codes below:
set $RUN_VERSION 'current';
rewrite_by_lua_block {
....
ngx.var.RUN_VERSION = 'new'
}
echo $RUN_VERSION;
--------------------
visit the server: new
It seems work.
========================================================
set $RUN_VERSION 'current';
rewrite_by_lua_block {
....
ngx.var.RUN_VERSION = 'new'
}
echo $RUN_VERSION;
if ($RUN_VERSION = "new"){
echo "11111";
}
if ($RUN_VERSION = "current"){
echo "22222";
}
--------------------
visit the server: 22222
Problems in the 2nd case:
1. echo $RUN_VERSION; shows nothing.
2. It was supposed to display 11111

The easiest fix is turning on rewrite_by_lua_no_postpone in your nginx configuration.
I assume there's some reason (maybe performance?) why this is disabled by default.

Related

Nginx location with regex in the middle of path

i'm trying to configure Nginx to parse correctly two similar paths with a regex in it.
I have 2 services:
/api/v1/myservice/{var}/client
/api/v1/myservice/{var}/client/detail
{var} is a variable I'm reading which is working
Here my code (not using the var here):
location ~ /api/v1/myservice/(.*)/client {
return 200 "service 1 ";
}
location ~ /api/v1/myservice/(.*)/client/detail {
return 200 "service 2";
}
When I run curl like the following:
curl http://localhost/api/v1/myservice/623723852/client/detail
I see the response from the first location:
"Service 1"
What Am I doing wrong?
thanks
No need to have nested locations as in the other answer. The nested locations in NGINX often give a false impression about directives' inheritance. More often than not, they are not inherited, because NGINX chooses one specific location anyway. For these reasons, if you can, avoid nested locations.
You can simply change the order of locations so that the more specific one goes first. NGINX will match the one that appears first in the config (if they both match):
location ~ /api/v1/myservice/(.*)/client/detail {
return 200 "service 2";
}
location ~ /api/v1/myservice/(.*)/client {
return 200 "service 1 ";
}
Alternatively, somewhat better (for performance reasons), you can adjust your regexes for more exact matching. Then the placement in configuration won't matter:
location ~ ^/api/v1/myservice/(.*)/client$ {
return 200 "service 1 ";
}
location ~ ^/api/v1/myservice/(.*)/client/detail$ {
return 200 "service 2";
}

how to check if Database is empty swift 3 - Firebase

Im trying to create an if statement to check if a database in Firebase is empty or haven't been create it . Any ideas how can I check that ? The app works when I press a button all the data that I have saved will be upload it to the database before I press that button the database is empty.
Thank you
For that you need to check snapshot children count first, whether it's empty or not.
For that -
var ref = Firebase(url: FIREBASE_URL)
ref.observeEventType(.Value, withBlock: { (snapshot: FDataSnapshot!) in
if(snapshot.childrenCount > 0){ // It has value already
}
else{ // Still Empty
// Upload values to firebase
}
})
Hope it will help you to achieve what you want. :)
Thank you Amir and Frank , I figure it out. I had to update Amir code a little bit , but I got it to work . Here is the details
let ref = Database.database().reference(fromURL: "DATABASE_PATH")
ref.observe(.value, with: { (snapshot: DataSnapshot!) in
if(snapshot.childrenCount > 0){ // It has value already
print(" value")
}
else{ // Still Empty
print("No value")
}
})

getGraphEdge getGraphNode - What's the difference and when to use it?

I'm confused by those both.
In my case, I try to get data from a FB page and got this code:
try {
$response = $fb->get('/' . $sPageID . '?fields=posts', $_SESSION['facebook_access_token']);
$responseFeed = $fb->get('/' . $sPageID . '/feed', $_SESSION['facebook_access_token']);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "catch";
dd($e->getMessage());
}
$graphEdge = $responseFeed->getGraphEdge();
$tester = $response->getGraphNode();
echo "<pre>";
print_r ( $tester );
echo "</pre>";
$response and $responseFeed contain data. But only $tester contains data later on. $graphEdge is empty.
$tester offers a method: getParentGraphEdge() but the return value is empty as well. That happens on different URLs like /pageID or /pageID?field=posts or /pageID/feed
When to use getGraphEdge? And why doesn't it work here?
How you use thoses functions is the right way. Use getGraphNode() when you request a node, and getGraphEdge() for an edge.
I don't know why the edge is empty, but try to execute your request (/{page-id}/feed) on the Graph API Explorer with the access token in $_SESSION['facebook_access_token'], maybe its result is just empty.

Perl taint mode with domain name input for CGI resulting in “Insecure dependency in eval”

Given the following in a CGI script with Perl and taint mode I have not been able to get past the following.
tail /etc/httpd/logs/error_log
/usr/local/share/perl5/Net/DNS/Dig.pm line 906 (#1)
(F) You tried to do something that the tainting mechanism didn't like.
The tainting mechanism is turned on when you're running setuid or
setgid, or when you specify -T to turn it on explicitly. The
tainting mechanism labels all data that's derived directly or indirectly
from the user, who is considered to be unworthy of your trust. If any
such data is used in a "dangerous" operation, you get this error. See
perlsec for more information.
[Mon Jan 6 16:24:21 2014] dig.cgi: Insecure dependency in eval while running with -T switch at /usr/local/share/perl5/Net/DNS/Dig.pm line 906.
Code:
#!/usr/bin/perl -wT
use warnings;
use strict;
use IO::Socket::INET;
use Net::DNS::Dig;
use CGI;
$ENV{"PATH"} = ""; # Latest attempted fix
my $q = CGI->new;
my $domain = $q->param('domain');
if ( $domain =~ /(^\w+)\.(\w+\.?\w+\.?\w+)$/ ) {
$domain = "$1\.$2";
}
else {
warn("TAINTED DATA SENT BY $ENV{'REMOTE_ADDR'}: $domain: $!");
$domain = ""; # successful match did not occur
}
my $dig = new Net::DNS::Dig(
Timeout => 15, # default
Class => 'IN', # default
PeerAddr => $domain,
PeerPort => 53, # default
Proto => 'UDP', # default
Recursion => 1, # default
);
my #result = $dig->for( $domain, 'NS' )->to_text->rdata();
#result = sort #result;
print #result;
I normally use Data::Validate::Domain to do checking for a “valid” domain name, but could not deploy it in a way in which the tainted variable error would not occur.
I read that in order to untaint a variable you have to pass it through a regex with capture groups and then join the capture groups to sanitize it. So I deployed $domain =~ /(^\w+)\.(\w+\.?\w+\.?\w+)$/. As shown here it is not the best regex for the purpose of untainting a domain name and covering all possible domains but it meets my needs. Unfortunately my script is still producing tainted failures and I can not figure out how.
Regexp-Common does not provide a domain regex and modules don’t seem to work with untainting variable so I am at a loss now.
How to get this thing to pass taint checking?
$domain is not tainted
I verified that your $domain is not tainted. This is the only variable you use that could be tainted, in my opinion.
perl -T <(cat <<'EOF'
use Scalar::Util qw(tainted);
sub p_t($) {
if (tainted $_[0]) {
print "Tainted\n";
} else {
print "Not tainted\n";
}
}
my $domain = shift;
p_t($domain);
if ($domain =~ /(^\w+)\.(\w+\.?\w+\.?\w+)$/) {
$domain = "$1\.$2";
} else {
warn("$domain\n");
$domain = "";
}
p_t($domain);
EOF
) abc.def
It prints
Tainted
Not tainted
What Net::DNS::Dig does
See Net::DNS::Dig line 906. It is the beginning of to_text method.
sub to_text {
my $self = shift;
my $d = Data::Dumper->new([$self],['tobj']);
$d->Purity(1)->Deepcopy(1)->Indent(1);
my $tobj;
eval $d->Dump; # line 906
…
From new definition I know that $self is just hashref containing values from new parameters and several other filled in the constructor. The evaled code produced by $d->Dump is setting $tobj to a deep copy of $self (Deepcopy(1)), with correctly set self-references (Purity(1)) and basic pretty-printing (Indent(1)).
Where is the problem, how to debug
From what I found out about &Net::DNS::Dig::to_text, it is clear that the problem is at least one tainted item inside $self. So you have a straightforward way to debug your problem further: after constructing the $dig object in your script, check which of its items is tainted. You can dump the whole structure to stdout using print Data::Dumper::Dump($dig);, which is roughly the same as the evaled code, and check suspicious items using &Scalar::Util::tainted.
I have no idea how far this is from making Net::DNS::Dig work in taint mode. I do not use it, I was just curious and wanted to find out, where the problem is. As you managed to solve your problem otherwise, I leave it at this stage, allowing others to continue debugging the issue.
As resolution to this question if anyone comes across it in the future it was indeed the module I was using which caused the taint checks to fail. Teaching me an important lesson on trusting modules in a CGI environment. I switched to Net::DNS as I figured it would not encounter this issue and sure enough it does not. My code is provided below for reference in case anyone wants to accomplish the same thing I set out to do which is: locate the nameservers defined for a domain within its own zone file.
#!/usr/bin/perl -wT
use warnings;
use strict;
use IO::Socket::INET;
use Net::DNS;
use CGI;
$ENV{"PATH"} = ""; // Latest attempted fix
my $q = CGI->new;
my $domain = $q->param('domain');
my #result;
if ( $domain =~ /(^\w+)\.(\w+\.?\w+\.?\w+)$/ ) {
$domain = "$1\.$2";
}
else {
warn("TAINTED DATA SENT BY $ENV{'REMOTE_ADDR'}: $domain: $!");
$domain = ""; # successful match did not occur
}
my $ip = inet_ntoa(inet_aton($domain));
my $res = Net::DNS::Resolver->new(
nameservers => [($ip)],
);
my $query = $res->query($domain, "NS");
if ($query) {
foreach my $rr (grep { $_->type eq 'NS' } $query->answer) {
push(#result, $rr->nsdname);
}
}
else {
warn "query failed: ", $res->errorstring, "\n";
}
#result = sort #result;
print #result;
Thanks for the comments assisting me in this matter, and SO for teaching more then any other resource I have come across.

Query Facebook Opengraph next page parameters

I am unable to implement pagination with Facebook OpenGraph. I have exhausted every option I have found.
My hope is to query for 500 listens repeatedly until there are none left. However, I am only able to receive a response from my first query. Below is my current code, but I have tried setting the parameters to different amounts rather than having the fields from the [page][next] dictate them
$q_param['limit'] = 500;
$next_exists = true;
while($next_exists){
$music = $facebook->api('/me/music.listens','GET', $q_param);
$music_data = array_merge($music_data, $music['data']);
if($music["paging"]["next"]==null || $music["paging"]["next"]=="")
$next_exists = false;
else{
$url = $music["paging"]["next"];
parse_str(parse_url($url, PHP_URL_QUERY), $array);
foreach ($array as $key => $value) {
$q_param[$key]=$value;
}
}
}
}
a - Can you please share what do you get after first call?
b - Also, possible if you can share the whole file?
I think your script is timing out. Try adding following on top of your file:
set_time_limit(0);
Can you check apache log files?
sudo tail -f /var/log/apache2/error.log