[121] | 1 | #------------------------------------------------------------------------------- |
---|
| 2 | # This file is part of the FLARM¨-Radar Project. |
---|
| 3 | # |
---|
| 4 | # Copyright 2013 Netzschmiede GmbH (http://www.netzschmiede.ch) |
---|
| 5 | # |
---|
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 7 | # you may not use this file except in compliance with the License. |
---|
| 8 | # You may obtain a copy of the License at |
---|
| 9 | # |
---|
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | # |
---|
| 12 | # Unless required by applicable law or agreed to in writing, software |
---|
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | # See the License for the specific language governing permissions and |
---|
| 16 | # limitations under the License. |
---|
| 17 | # |
---|
| 18 | # Project Website: www.flarmradar.ch |
---|
| 19 | # Email: info@flarmradar.ch |
---|
| 20 | #------------------------------------------------------------------------------- |
---|
[103] | 21 | #!/usr/bin/perl |
---|
| 22 | |
---|
| 23 | use strict; |
---|
| 24 | use Getopt::Std; |
---|
| 25 | # load LWP library: |
---|
| 26 | use LWP::UserAgent; |
---|
| 27 | use HTML::Parse; |
---|
| 28 | use Time::HiRes; |
---|
| 29 | |
---|
| 30 | # parse options |
---|
| 31 | my %options; |
---|
| 32 | getopts('f:u:s:', \%options); |
---|
| 33 | unless(defined($options{'f'}) && defined($options{u})) { |
---|
| 34 | usage(); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | my $url = $options{u}; |
---|
| 38 | my $file = $options{f}; |
---|
| 39 | my $speed = defined($options{s}) ? $options{s} : 1; |
---|
| 40 | if (! -f $file || ! -w $file) { |
---|
| 41 | print "error reading input file: ", $file, "\n"; |
---|
| 42 | exit 1; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | # create UserAgent object |
---|
| 49 | my $ua = new LWP::UserAgent; |
---|
| 50 | |
---|
| 51 | # set a user agent (browser-id) |
---|
| 52 | # $ua->agent('Mozilla/5.5 (compatible; MSIE 5.5; Windows NT 5.1)'); |
---|
| 53 | |
---|
| 54 | # timeout: |
---|
| 55 | $ua->timeout(15); |
---|
| 56 | |
---|
| 57 | my $i = 0; |
---|
| 58 | my $buf; |
---|
| 59 | |
---|
| 60 | open(FILE, "< $file") || die("error while reading file ", $file); |
---|
| 61 | while (my $record = <FILE>) { |
---|
| 62 | chomp($record); |
---|
| 63 | if ($record =~ /^\$GPGGA,/) { |
---|
| 64 | flush($buf, $url, $i); |
---|
| 65 | #sleep(1); |
---|
| 66 | Time::HiRes::sleep(1/$speed); |
---|
| 67 | $buf = undef; |
---|
| 68 | } |
---|
| 69 | $i++; |
---|
| 70 | $buf = (defined($buf)) ? "$buf;$record" : $record; |
---|
| 71 | } |
---|
| 72 | flush($buf, $url, $i); |
---|
| 73 | close(FILE); |
---|
| 74 | |
---|
| 75 | exit 0; |
---|
| 76 | |
---|
| 77 | sub flush() { |
---|
| 78 | my ($records, $url, $count) = @_; |
---|
| 79 | my $request = HTTP::Request->new('PUT'); |
---|
| 80 | $request->url($url); |
---|
| 81 | $request->content($records); |
---|
| 82 | print "flushing at $count\n"; |
---|
| 83 | # run the request |
---|
| 84 | my $response = $ua->request($request); |
---|
| 85 | my $code = $response->code; |
---|
| 86 | die("error processing records ", $records) if (200 != $response->code); |
---|
| 87 | } |
---|
| 88 | # proceed the request: |
---|
| 89 | my $request = HTTP::Request->new('PUT'); |
---|
| 90 | $request->url($url); |
---|
| 91 | $request->content("blubb"); |
---|
| 92 | |
---|
| 93 | my $response = $ua->request($request); |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | # |
---|
| 97 | # responses: |
---|
| 98 | # |
---|
| 99 | |
---|
| 100 | # response code (like 200, 404, etc) |
---|
| 101 | my $code = $response->code; |
---|
| 102 | print "Return code: ", $code, "\n"; |
---|
| 103 | |
---|
| 104 | # headers (Server: Apache, Content-Type: text/html, ...) |
---|
| 105 | my $headers = $response->headers_as_string; |
---|
| 106 | print "Headers: ", $headers, "\n"; |
---|
| 107 | |
---|
| 108 | # HTML body: |
---|
| 109 | my $body = $response->content; |
---|
| 110 | print "Body: ", $body, "\n"; |
---|
| 111 | |
---|
| 112 | sub usage() { |
---|
| 113 | print "usage: bulb\n"; |
---|
| 114 | exit 1; |
---|
| 115 | } |
---|