Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
proxy-by-url
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
proxy-by-url
Commits
d23f3e75
Commit
d23f3e75
authored
Dec 01, 2011
by
Cédric de Saint Martin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for file based url list
parent
30ef9e4f
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
51 additions
and
22 deletions
+51
-22
index.js
index.js
+51
-22
No files found.
index.js
View file @
d23f3e75
...
@@ -4,33 +4,61 @@
...
@@ -4,33 +4,61 @@
// an example of how to write a middleware.
// an example of how to write a middleware.
//
//
function
matcher
(
url
,
dest
)
{
var
fs
=
require
(
'
fs
'
);
// First, turn the URL into a regex.
// NOTE: Turning user input directly into a Regular Expression is NOT SAFE.
function
getMatchers
(
urls
)
{
var
r
=
new
RegExp
(
url
.
replace
(
/
\/
/
,
'
\\
/
'
));
var
matchers
=
[],
// This next block of code may look a little confusing.
matcher
,
// It returns a closure (anonymous function) for each URL to be matched,
url
,
// storing them in an array - on each request, if the URL matches one that has
r
;
// a function stored for it, the function will be called.
return
function
(
url
)
{
for
(
url
in
urls
)
{
var
m
=
url
.
match
(
r
);
// Call the 'matcher' function above, and store the resulting closure.
if
(
!
m
)
{
// First, turn the URL into a regex.
return
;
// NOTE: Turning user input directly into a Regular Expression is NOT SAFE.
}
r
=
new
RegExp
(
url
.
replace
(
/
\/
/
,
'
\\
/
'
));
var
path
=
url
.
slice
(
m
[
0
].
length
);
// This next block of code may look a little confusing.
console
.
log
(
'
proxy:
'
,
url
,
'
->
'
,
dest
);
// It returns a closure (anonymous function) for each URL to be matched,
return
{
url
:
path
,
dest
:
dest
};
// storing them in an array - on each request, if the URL matches one that has
// a function stored for it, the function will be called.
matcher
=
(
function
(
r
)
{
var
dest
=
urls
[
url
];
return
function
(
url
)
{
var
m
=
url
.
match
(
r
);
if
(
!
m
)
{
return
;
}
var
path
=
url
.
slice
(
m
[
0
].
length
);
console
.
log
(
'
proxy:
'
,
url
,
'
->
'
,
dest
);
return
({
url
:
path
,
dest
:
dest
});
}})(
r
);
matchers
.
push
(
matcher
);
}
}
return
matchers
;
}
}
module
.
exports
=
function
(
urls
)
{
module
.
exports
=
function
(
urls
)
{
// This is the entry point for our middleware.
var
matchers
,
// 'matchers' is the array of URL matchers, as mentioned above.
urlFile
;
var
matchers
=
[];
if
(
typeof
urls
===
'
string
'
)
{
for
(
var
url
in
urls
)
{
//
// Call the 'matcher' function above, and store the resulting closure.
// If we are passed a string then assume it is a
matchers
.
push
(
matcher
(
url
,
urls
[
url
]));
// file path, parse that file and watch it for changes
//
urlsFile
=
urls
,
urls
=
JSON
.
parse
(
fs
.
readFileSync
(
urlsFile
));
fs
.
watchFile
(
urlsFile
,
function
()
{
console
.
log
(
"
Reloading urls...
"
);
fs
.
readFile
(
urlsFile
,
function
(
err
,
data
)
{
if
(
err
)
{
self
.
emit
(
'
error
'
,
err
);
}
urls
=
JSON
.
parse
(
data
);
matchers
=
getMatchers
(
urls
);
});
});
}
}
matchers
=
getMatchers
(
urls
);
// This closure is returned as the request handler.
// This closure is returned as the request handler.
middleware
=
function
(
req
,
res
,
next
)
{
middleware
=
function
(
req
,
res
,
next
)
{
...
@@ -61,6 +89,7 @@ module.exports = function (urls) {
...
@@ -61,6 +89,7 @@ module.exports = function (urls) {
//
//
// Same as above, but for WebSocket
// Same as above, but for WebSocket
//
//
console
.
log
(
"
Websocket proxying :
"
+
req
.
url
);
var
proxy
=
next
;
var
proxy
=
next
;
for
(
var
k
in
matchers
)
{
for
(
var
k
in
matchers
)
{
// for each URL matcher, try the request's URL.
// for each URL matcher, try the request's URL.
...
...
Write
Preview
Markdown
is supported
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