Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Jochen Bauer
uWebSockets
Commits
e402f022
Commit
e402f022
authored
Aug 10, 2017
by
Alex Hultman
Browse files
Rewrite uri parser (ipv6, non-numeric port crash)
parent
7facffcf
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/Hub.cpp
View file @
e402f022
...
...
@@ -84,43 +84,80 @@ uS::Socket *allocateHttpSocket(uS::Socket *s) {
return
(
uS
::
Socket
*
)
new
HttpSocket
<
CLIENT
>
(
s
);
}
void
Hub
::
connect
(
std
::
string
uri
,
void
*
user
,
std
::
map
<
std
::
string
,
std
::
string
>
extraHeaders
,
int
timeoutMs
,
Group
<
CLIENT
>
*
eh
)
{
if
(
!
eh
)
{
eh
=
(
Group
<
CLIENT
>
*
)
this
;
bool
parseURI
(
std
::
string
&
uri
,
bool
&
secure
,
std
::
string
&
hostname
,
int
&
port
,
std
::
string
&
path
)
{
port
=
80
;
secure
=
false
;
size_t
offset
=
5
;
if
(
!
uri
.
compare
(
0
,
6
,
"wss://"
))
{
port
=
443
;
secure
=
true
;
offset
=
6
;
}
else
if
(
uri
.
compare
(
0
,
5
,
"ws://"
))
{
return
false
;
}
size_t
offset
=
0
;
std
::
string
protocol
=
uri
.
substr
(
offset
,
uri
.
find
(
"://"
)),
hostname
,
portStr
,
path
;
if
((
offset
+=
protocol
.
length
()
+
3
)
<
uri
.
length
())
{
hostname
=
uri
.
substr
(
offset
,
uri
.
find_first_of
(
":/"
,
offset
)
-
offset
);
if
(
offset
==
uri
.
length
())
{
return
false
;
}
offset
+=
hostname
.
length
();
if
(
uri
[
offset
]
==
':'
)
{
offset
++
;
portStr
=
uri
.
substr
(
offset
,
uri
.
find
(
"/"
,
offset
)
-
offset
);
if
(
uri
[
offset
]
==
'['
)
{
if
(
++
offset
==
uri
.
length
())
{
return
false
;
}
offset
+=
portStr
.
length
();
if
(
uri
[
offset
]
==
'/'
)
{
path
=
uri
.
substr
(
++
offset
);
size_t
endBracket
=
uri
.
find
(
']'
,
offset
);
if
(
endBracket
==
std
::
string
::
npos
)
{
return
false
;
}
hostname
=
uri
.
substr
(
offset
,
endBracket
-
offset
);
offset
=
endBracket
+
1
;
}
else
{
hostname
=
uri
.
substr
(
offset
,
uri
.
find_first_of
(
":/"
,
offset
)
-
offset
);
offset
+=
hostname
.
length
();
}
if
(
hostname
.
length
())
{
int
port
=
80
;
bool
secure
=
false
;
if
(
protocol
==
"wss"
)
{
port
=
443
;
secure
=
true
;
}
else
if
(
protocol
!=
"ws"
)
{
eh
->
errorHandler
(
user
);
return
;
}
if
(
offset
==
uri
.
length
())
{
path
.
clear
();
return
true
;
}
if
(
uri
[
offset
]
==
':'
)
{
offset
++
;
std
::
string
portStr
=
uri
.
substr
(
offset
,
uri
.
find
(
'/'
,
offset
)
-
offset
);
if
(
portStr
.
length
())
{
port
=
stoi
(
portStr
);
try
{
port
=
stoi
(
portStr
);
}
catch
(...)
{
return
false
;
}
}
else
{
return
false
;
}
offset
+=
portStr
.
length
();
}
if
(
offset
==
uri
.
length
())
{
path
.
clear
();
return
true
;
}
if
(
uri
[
offset
]
==
'/'
)
{
path
=
uri
.
substr
(
++
offset
);
}
return
true
;
}
void
Hub
::
connect
(
std
::
string
uri
,
void
*
user
,
std
::
map
<
std
::
string
,
std
::
string
>
extraHeaders
,
int
timeoutMs
,
Group
<
CLIENT
>
*
eh
)
{
if
(
!
eh
)
{
eh
=
(
Group
<
CLIENT
>
*
)
this
;
}
int
port
;
bool
secure
;
std
::
string
hostname
,
path
;
if
(
!
parseURI
(
uri
,
secure
,
hostname
,
port
,
path
))
{
eh
->
errorHandler
(
user
);
}
else
{
HttpSocket
<
CLIENT
>
*
httpSocket
=
(
HttpSocket
<
CLIENT
>
*
)
uS
::
Node
::
connect
<
allocateHttpSocket
,
onClientConnection
>
(
hostname
.
c_str
(),
port
,
secure
,
eh
);
if
(
httpSocket
)
{
// startTimeout occupies the user
...
...
@@ -147,8 +184,6 @@ void Hub::connect(std::string uri, void *user, std::map<std::string, std::string
}
else
{
eh
->
errorHandler
(
user
);
}
}
else
{
eh
->
errorHandler
(
user
);
}
}
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment