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
|
package OpenSim::Utility;
use strict;
use XML::RPC;
use XML::Simple;
use Data::UUID;
use DBHandler;
use OpenSim::Config;
use Socket;
sub XMLRPCCall {
my ($url, $methodname, $param) = @_;
my $xmlrpc = new XML::RPC($url);
my $result = $xmlrpc->call($methodname, $param);
return $result;
}
sub XMLRPCCall_array {
my ($url, $methodname, $param) = @_;
my $xmlrpc = new XML::RPC($url);
my $result = $xmlrpc->call($methodname, @$param);
return $result;
}
sub UIntsToLong {
my ($int1, $int2) = @_;
return $int1 * 4294967296 + $int2;
}
sub getSimpleResult {
my ($sql, @args) = @_;
my $dbh = &DBHandler::getConnection($OpenSim::Config::DSN, $OpenSim::Config::DBUSER, $OpenSim::Config::DBPASS);
my $st = new Statement($dbh, $sql);
return $st->exec(@args);
}
sub GenerateUUID {
my $ug = new Data::UUID();
my $uuid = $ug->create();
return $ug->to_string($uuid);
}
sub ZeroUUID {
return "00000000-0000-0000-0000-000000000000";
}
sub HEX2UUID {
my $hex = shift;
Carp::croak("$hex is not a uuid") if (length($hex) != 32);
my @sub_uuids = ($hex =~ /(\w{8})(\w{4})(\w{4})(\w{4})(\w{12})/);
return join("-", @sub_uuids);
}
sub BIN2UUID {
# TODO:
}
sub UUID2HEX {
my $uuid = shift;
$uuid =~ s/-//g;
return $uuid;
}
sub UUID2BIN {
my $uuid = shift;
return pack("H*", &UUID2HEX($uuid));
}
sub HttpPostRequest {
my ($url, $postdata) = @_;
$url =~ /http:\/\/([^:\/]+)(:([0-9]+))?(\/.*)?/;
my ($host, $port, $path) = ($1, $3, $4);
$port ||= 80;
$path ||= "/";
my $CRLF= "\015\012";
my $addr = (gethostbyname($host))[4];
my $name = pack('S n a4 x8', 2, $port, $addr);
my $data_len = length($postdata);
socket(SOCK, PF_INET, SOCK_STREAM, 0);
connect(SOCK, $name) ;
select(SOCK); $| = 1; select(STDOUT);
print SOCK "POST $path HTTP/1.0$CRLF";
print SOCK "Host: $host:$port$CRLF";
print SOCK "Content-Length: $data_len$CRLF";
print SOCK "$CRLF";
print SOCK $postdata;
my $ret = "";
unless (<SOCK>) {
close(SOCK);
Carp::croak("can not connect to $url");
}
my $header = "";
while (<SOCK>) {
$header .= $_;
last if ($_ eq $CRLF);
}
if ($header != /200/) {
return $ret;
}
while (<SOCK>) {
$ret .= $_;
}
return $ret;
}
# TODO : merge with POST
sub HttpGetRequest {
my ($url) = @_;
$url =~ /http:\/\/([^:\/]+)(:([0-9]+))?(\/.*)?/;
my ($host, $port, $path) = ($1, $3, $4);
$port ||= 80;
$path ||= "/";
my $CRLF= "\015\012";
my $addr = (gethostbyname($host))[4];
my $name = pack('S n a4 x8', 2, $port, $addr);
socket(SOCK, PF_INET, SOCK_STREAM, 0);
connect(SOCK, $name) ;
select(SOCK); $| = 1; select(STDOUT);
print SOCK "GET $path HTTP/1.0$CRLF";
print SOCK "Host: $host:$port$CRLF";
print SOCK "$CRLF";
unless (<SOCK>) {
close(SOCK);
Carp::croak("can not connect to $url");
}
while (<SOCK>) {
last if ($_ eq $CRLF);
}
my $ret = "";
while (<SOCK>) {
$ret .= $_;
}
return $ret;
}
sub XML2Obj {
my $xml = shift;
my $xs = new XML::Simple( keyattr=>[] );
return $xs->XMLin($xml);
}
sub Log {
my $server_name = shift;
my @param = @_;
open(FILE, ">>" . $OpenSim::Config::DEBUG_LOGDIR . "/" . $server_name . ".log");
foreach(@param) {
print FILE $_ . "\n";
}
print FILE "<<<<<<<<<<<=====================\n\n";
close(FILE);
}
1;
|