#!/usr/bin/env perl

# license note
# Copyright (c) 2014, Eliezer Croitoru
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE

use MIME::Base64;
use LWP::UserAgent;
use URI::Escape;
use strict;

my $encoded;
my @vals;
my $string;
my $encoded;
my $content;
my $debug = "False";

$| = 1;
 
my $ua = LWP::UserAgent->new;
my $server_endpoint = "http://www2.ngtech.co.il:8080/StoreID/";
my $escaped;

while (1) {
  $string = <>;
  @vals = split(' ', $string);
  $escaped =  uri_escape(@vals[1]);
  if ($debug eq "True") {
    print STDERR "STDERR: " . $escaped ;
  }
  my $req = HTTP::Request->new(POST => $server_endpoint);
  my $post_data = "url=$escaped&cid=123456";
  $req->content($post_data);
 
  my $resp = $ua->request($req);
  my $message;
  if ($resp->is_success) {
    $message = $resp->decoded_content;
    if ($debug eq "True") {
      print STDERR "Received reply: $message\n";
    }
  }
  else {
    $message = "ERR details=Error_accured";
    if ($debug eq "True") {
      print STDERR "HTTP POST error code: ", $resp->code, "\n";
      print STDERR "HTTP POST error message: ", $resp->message, "\n";
	}
  }
  print @vals[0] . " " . $message . "\n";
}
