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