-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubserver.pl
236 lines (188 loc) · 7.43 KB
/
subserver.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env perl
# directions: on fi /usr/local/bin/subserver.pl (server port 8081 and vlc on 8082)
$HELP1="Easiest to invoke this from same directory that files reside. open browser to http://fi:8081/subs.html";
$HELP2="invoke vlc with subvlc alias on linux (linux: /usr/bin/vlc --http-host=127.0.0.1 --http-port=8080)\n on windows cd \program files (x86))\VIdeoLAN/VLC and invoke: vlc.exe --http-host=127.0.0.1 --http-port=8080 ";
use LWP;
use LWP::UserAgent;
use Socket;
use IO::Socket;
my $port = 8081; # shift;
# choose where VLC is running
#$VLC='http://127.0.0.1:8082'; # fi
$VLC='http://192.168.1.22:8080'; # laptop
$VLC='http://10.0.0.235:8080'; # laptop at Argen
$VLC='http://127.0.0.1:8080'; # laptop at Argen
$USER = '';
$PASS = 'f';
$SHOWFILES=0;
$DOCUMENT_ROOT = ".";
#$DOCUMENT_ROOT = "/var/www/html";
my $ua = LWP::UserAgent->new();
sub parse_form {
my $data = $_[0];
my %data;
foreach (split /&/, $data) {
my ($key, $val) = split /=/;
$val =~ s/\+/ /g;
$val =~ s/%(..)/chr(hex($1))/eg;
$data{$key} = $val;
}
return %data;
}
sub vlcpause {
my $request = HTTP::Request->new(GET => "$VLC/requests/status.xml?command=pl_pause");
$request->authorization_basic($USER, $PASS);
my $response = $ua->request($request);
my $content = $response->content();
}
sub vlcseek {
($ms) = $_[0]/1000;
($ms) = split(/\./, $ms);
my $request = HTTP::Request->new(GET => "$VLC/requests/status.xml?command=seek&val=$ms");
$request->authorization_basic($USER, $PASS);
my $response = $ua->request($request);
}
# Setup and create socket
defined($port) or die "Usage: $0 portno\n";
my $server = new IO::Socket::INET(Proto => 'tcp',
LocalPort => $port,
Listen => SOMAXCONN,
Reuse => 1);
$server or die "Unable to create server socket: $!" ;
my $VLCsocket = new IO::Socket::INET(Proto => 'tcp',
LocalPort => 8080,
Listen => SOMAXCONN,
Reuse => 1);
$VLCsocket or die "Unable to create vlc socket: $!" ;
# Await requests and handle them as they arrive
print "VLC command server is running on port $port for vlc on $VLC...\n";
print "$HELP1\n";
print "$HELP2\n";
while (my $client = $server->accept()) {
binmode $client;
$client->autoflush(1);
my %request = ();
my %data;
{
#-------- Read Request ---------------
local $/ = Socket::CRLF;
while (<$client>) {
chomp; # Main http request
if (/\s*(\w+)\s*([^\s]+)\s*HTTP\/(\d.\d)/) {
$request{METHOD} = uc $1;
$request{URL} = $2;
$request{HTTP_VERSION} = $3;
} # Standard headers
elsif (/:/) {
(my $type, my $val) = split /:/, $_, 2;
$type =~ s/^\s+//;
foreach ($type, $val) {
s/^\s+//;
s/\s+$//;
}
$request{lc $type} = $val;
} # POST data
elsif (/^$/) {
read($client, $request{CONTENT}, $request{'content-length'})
if defined $request{'content-length'};
last;
}
}
}
#-------- SORT OUT METHOD ---------------
#print ("kwrr1 method=>" . $request{METHOD} . "< ====>" . $request{CONTENT} . "<\n");
if ($request{METHOD} eq 'GET') {
if ($request{URL} =~ /(.*)\?(.*)/) {
$request{URL} = $1;
$request{CONTENT} = $2;
%data = parse_form($request{CONTENT});
} else {
%data = ();
}
$data{"_method"} = "GET";
} elsif ($request{METHOD} eq 'POST') {
%data = parse_form($request{CONTENT});
$data{"_method"} = "POST";
} else {
$data{"_method"} = "ERROR";
}
#------- Serve file ----------------------
#print ("kwrr2 req=>" . $request{URL} . "<\n");
if ($request{URL} =~ /vlcpause/) {
print "processing vlc pause: $request{URL}\n";
&vlcpause();
print $client "HTTP/1.0 200 OK", Socket::CRLF;
#print $client "Content-type: text/html", Socket::CRLF;
} elsif ($request{URL} =~ /vlcseek/) {
print "processing vlc seek for $request{CONTENT}\n";
#($command, $param) = split(/\?/, $request{URL});
&vlcseek($request{CONTENT});
print $client "HTTP/1.0 200 OK", Socket::CRLF;
} elsif ($request{URL} =~ /getdir/) {
print "processing getdir\n";
print $client "HTTP/1.0 200 OK", Socket::CRLF;
$dirlist='';
(@files) = glob "$DOCUMENT_ROOT/*.srt";
for $fname (@files) {
@arr = split /\//, $fname ; # trim path from basename
#$dirlist .= "$fname,";
$dirlist .= "$arr[$#arr],";
}
chop $dirlist;
print $client "Content-type: text/plain", Socket::CRLF;
print $client Socket::CRLF;
my $buffer;
print $client $dirlist;
$data{"_status"} = "200";
print "processed getdir\n";
} else {
$request{URL} = '/index.html' if $request{URL} eq "/";
print "Serving $request{URL}";
#http://127.0.0.1:8081/requests/status.xml?command=pl_pause
# my $localfile = $DOCUMENT_ROOT.$request{URL};
my $localfile = "$DOCUMENT_ROOT" . "$request{URL}";
# Send Response
if (open(FILE, "<$localfile")) {
binmode FILE;
@arr = (split /\./, $localfile);
$extension = "$arr[$#arr]"; # https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
if ($extension eq 'css') {
$mimetype = 'text/css';
} elsif ($extension eq 'htm' || $extension eq 'html') {
$mimetype = 'text/html';
} elsif ($extension eq 'js' ) {
$mimetype = 'text/javascript';
} elsif ($extension eq 'png' ) {
$mimetype = 'image/png';
} else {
$mimetype = 'text/plain';
}
print "\n!!!!ext=>$extension< mimetype=>$mimetype< \n";
print "...$localfile\n";
print $client "HTTP/1.0 200 OK", Socket::CRLF;
print $client "Content-type: $mimetype", Socket::CRLF;
print $client Socket::CRLF;
my $buffer;
while (read(FILE, $buffer, 4096)) {
print $client $buffer;
}
$data{"_status"} = "200";
}
else {
print "...ERROR: $localfile not found\n";
print $client "HTTP/1.0 404 Not Found", Socket::CRLF;
print $client Socket::CRLF;
print $client "<html><body>404 Not Found</body></html>";
$data{"_status"} = "404";
}
close(FILE);
# Log Request
if ($SHOWFILES) {
print ($DOCUMENT_ROOT.$request{URL},"\n");
foreach (keys(%data)) {
print (" $_ = $data{$_}\n"); }
}
}
# ----------- Close Connection and loop ------------------
close $client;
}