Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

chapter #1 #2

  • Loading branch information...
commit 5910712d6e3adfc8e37886f9b6ddea48383ba2a3 1 parent bf92504
@gomsun2 authored
Showing with 294 additions and 15 deletions.
  1. +38 −15 leanNodeBy50Examp/chapter_01/5.StreamingPerformanceOptimize/server.js
  2. +11 −0 leanNodeBy50Examp/chapter_01/6.DefenseCacheMemoryOverflower/content/index.html
  3. +6 −0 leanNodeBy50Examp/chapter_01/6.DefenseCacheMemoryOverflower/content/script.js
  4. +6 −0 leanNodeBy50Examp/chapter_01/6.DefenseCacheMemoryOverflower/content/styles.css
  5. +45 −0 leanNodeBy50Examp/chapter_01/6.DefenseCacheMemoryOverflower/server.js
  6. +11 −0 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content-pseudosafe/index.html-server
  7. +6 −0 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content-pseudosafe/subcontent/script.js-server
  8. +6 −0 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content-pseudosafe/subcontent/styles.css-server
  9. +10 −0 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content/index.html
  10. +1 −0  leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content/subcontent/script.js
  11. +1 −0  leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content/subcontent/styles.css
  12. +23 −0 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/protected_server.js
  13. +22 −0 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/pseudosafe_server.js
  14. +13 −0 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/server.js
  15. +24 −0 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/whitelist_server.js
  16. +13 −0 leanNodeBy50Examp/chapter_02/1.HandlingPostData/form.html
  17. +32 −0 leanNodeBy50Examp/chapter_02/1.HandlingPostData/server.js
  18. +13 −0 leanNodeBy50Examp/chapter_02/2.UsingConnect/form.html
  19. +13 −0 leanNodeBy50Examp/chapter_02/2.UsingConnect/server.js
View
53 leanNodeBy50Examp/chapter_01/5.StreamingPerformanceOptimize/server.js
@@ -8,20 +8,37 @@ var mineTypes = {
'.css':'text/css'
};
-var cache = {};
+var cache = {
+ store: {},
+ maxSize: 1024 * 1024 * 25,
+ maxAge: 1000 * 60 * 30 * 3, // 1hour 30min
+ cleanAfter: 1000 * 60 * 60 * 2, // 2hour
+ cleanedAt: 0,
+ clean: function(now){
+ if (now - this.cleanAfter > this.cleanedAt){
+ this.cleanedAt = now;
+ var that = this;
+ Object.keys(this.store).forEach(function(file){
+ if(now > that.store[file].timestamp + that.maxAge){
+ delete that.store[file];
+ }
+ });
+ }
+ }
+};
http.createServer(function(request, response){
var lookup = path.basename(decodeURI(request.url)) || 'index.html'
- ,filename = 'content/' + lookup;
- fs.exists(filename, function(exists){
- if(exists){
- var headers = {'Content-Type':mineTypes[path.extname(filename)]};
- if(cache[filename]){
+ , f = 'content/' + lookup;
+ fs.exists(f, function(exists){
+ if (exists){
+ var headers = {'Content-Type':mineTypes[path.extname(f)]};
+ if(cache[f]){
console.log('from cache');
response.writeHead(200, headers);
- response.end(cache[filename].content);
+ response.end(cache[f].content);
return;
}
- var stream = fs.createReadStream(filename).once('open', function(){
+ var s = fs.createReadStream(f).once('open', function(){
response.writeHead(200, headers);
this.pipe(response);
}).once('error', function(e){
@@ -29,17 +46,23 @@ http.createServer(function(request, response){
response.writeHead(500);
response.end('internal server error!!');
});
- fs.stat(filename, function(err, stats){
- var bufferOffset = 0;
- cache[filename] = {content: new Buffer(stats.size)};
- stream.on('data', function(chuck){
- chuck.copy(cache[filename].content, bufferOffset);
- bufferOffset += chuck.length;
- });
+ fs.stat(f, function(err, stats){
+ if (stats < cache.maxSize){
+ var bufferOffset = 0;
+ cache.store[f] = {
+ content: new Buffer(stats.size),
+ timestamp: Date.now()
+ };
+ s.on('data', function(data){
+ data.copy(cache.store[f].content, bufferOffset);
+ bufferOffset += data.length;
+ });
+ }
});
return;
}
response.writeHead(404);
response.end('page not found!');
+ cache.clean(Date.now());
});
}).listen(8080);
View
11 leanNodeBy50Examp/chapter_01/6.DefenseCacheMemoryOverflower/content/index.html
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Yay Node!</title>
+ <link rel="stylesheet" href="styles.css" type="text/css">
+ <script src="script.js" type="text/javascript"></script>
+</head>
+<body>
+<span id="yay">Yay!</span>
+</body>
+</html>
View
6 leanNodeBy50Examp/chapter_01/6.DefenseCacheMemoryOverflower/content/script.js
@@ -0,0 +1,6 @@
+/**
+ * Created by mac on 2014. 2. 5..
+ */
+window.onload=function(){
+ alert('Yay Node');
+};
View
6 leanNodeBy50Examp/chapter_01/6.DefenseCacheMemoryOverflower/content/styles.css
@@ -0,0 +1,6 @@
+#yay {
+ font-size: 5em;
+ background: blue;
+ color: yellow;
+ padding: 0.5em;
+}
View
45 leanNodeBy50Examp/chapter_01/6.DefenseCacheMemoryOverflower/server.js
@@ -0,0 +1,45 @@
+var http = require('http');
+var path = require('path');
+var fs = require('fs');
+
+var mineTypes = {
+ '.js':'text/javascript',
+ '.html':'text/html',
+ '.css':'text/css'
+};
+
+var cache = {};
+http.createServer(function(request, response){
+ var lookup = path.basename(decodeURI(request.url)) || 'index.html'
+ ,filename = 'content/' + lookup;
+ fs.exists(filename, function(exists){
+ if(exists){
+ var headers = {'Content-Type':mineTypes[path.extname(filename)]};
+ if(cache[filename]){
+ console.log('from cache');
+ response.writeHead(200, headers);
+ response.end(cache[filename].content);
+ return;
+ }
+ var stream = fs.createReadStream(filename).once('open', function(){
+ response.writeHead(200, headers);
+ this.pipe(response);
+ }).once('error', function(e){
+ console.log('fs.createReadStream error:' + e);
+ response.writeHead(500);
+ response.end('internal server error!!');
+ });
+ fs.stat(filename, function(err, stats){
+ var bufferOffset = 0;
+ cache[filename] = {content: new Buffer(stats.size)};
+ stream.on('data', function(chuck){
+ chuck.copy(cache[filename].content, bufferOffset);
+ bufferOffset += chuck.length;
+ });
+ });
+ return;
+ }
+ response.writeHead(404);
+ response.end('page not found!');
+ });
+}).listen(8080);
View
11 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content-pseudosafe/index.html-server
@@ -0,0 +1,11 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title>Yay Node!</title>
+ <link rel="stylesheet" href="subcontent/styles.css" type="text/css">
+ <script src="subcontent/script.js" type="text/javascript"></script>
+</head>
+<body>
+<span id="yay">Yay!</span>
+</body>
+</html>
View
6 ...eBy50Examp/chapter_01/7.ProtectFromHacking/content-pseudosafe/subcontent/script.js-server
@@ -0,0 +1,6 @@
+/**
+ * Created by mac on 2014. 2. 5..
+ */
+window.onload=function(){
+ alert('Yay Node');
+};
View
6 ...By50Examp/chapter_01/7.ProtectFromHacking/content-pseudosafe/subcontent/styles.css-server
@@ -0,0 +1,6 @@
+#yay {
+ font-size: 5em;
+ background: blue;
+ color: yellow;
+ padding: 0.5em;
+}
View
10 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content/index.html
@@ -0,0 +1,10 @@
+<html>
+<head>
+<title>Yay Node!</title>
+<link rel=stylesheet type=text/css href=subcontent/styles.css>
+<script src=subcontent/script.js type=text/javascript></script>
+</head>
+<body>
+<span id=yay>Yay!</span>
+</body>
+</html>
View
1  leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content/subcontent/script.js
@@ -0,0 +1 @@
+window.onload=function() {alert('Yay Node!');};
View
1  leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/content/subcontent/styles.css
@@ -0,0 +1 @@
+#yay {font-size:5em;background:blue;color:yellow;padding:0.5em}
View
23 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/protected_server.js
@@ -0,0 +1,23 @@
+
+var http = require('http');
+var path = require('path');
+var url = require('url');
+var fs = require('fs');
+
+http.createServer(function(request, response){
+ var lookup = url.parse(decodeURI(request.url)).pathname;
+ lookup = path.normalize(lookup);
+ lookup = (lookup === '/') ? '/index.html' : lookup;
+ var f = 'content' + lookup;
+ console.log(f);
+ fs.exists(f, function(exists){
+ if(!exists){
+ response.writeHead(404);
+ response.end('page not found');
+ return;
+ }
+ fs.readFile(f, function(err, data){
+ response.end(f);
+ });
+ });
+}).listen(8080);
View
22 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/pseudosafe_server.js
@@ -0,0 +1,22 @@
+
+var http = require('http');
+var path = require('path');
+var url = require('url');
+var fs = require('fs');
+
+http.createServer(function(request, response){
+ var lookup = url.parse(decodeURI(request.url)).pathname;
+ lookup = (lookup === '/') ? '/index.html-server' : lookup + '-server';
+ var f = 'content-pseudosafe' + lookup;
+ console.log(f);
+ fs.exists(f, function(exists){
+ if(!exists){
+ response.writeHead(404);
+ response.end('page not found');
+ return;
+ }
+ fs.readFile(f, function(err, data){
+ response.end(f);
+ });
+ });
+}).listen(8080);
View
13 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/server.js
@@ -0,0 +1,13 @@
+var http = require('http');
+var url = require('url');
+var fs = require('fs');
+
+http.createServer(function(request, response){
+ var lookup = url.parse(decodeURI(request.url)).pathname;
+ lookup = (lookup === '/') ? '/index.html' : lookup;
+ var f = 'content' + lookup;
+ console.log(f);
+ fs.readFile(f, function(err, data){
+ response.end(f);
+ });
+}).listen(8080);
View
24 leanNodeBy50Examp/chapter_01/7.ProtectFromHacking/whitelist_server.js
@@ -0,0 +1,24 @@
+var http = require('http');
+var url = require('url');
+var fs = require('fs');
+
+var whitelist = [
+ '/index.html',
+ '/subcontents/styles.css',
+ '/subcontents/script.js'
+];
+
+http.createServer(function(request, response){
+ var lookup = url.parse(decodeURI(request.url)).pathname;
+ lookup = (lookup === '/') ? '/index.html' : lookup;
+ var f = 'content' + lookup;
+ if (whitelist.indexOf(lookup) == -1){
+ response.writeHead(404);
+ response.end('page not found.');
+ return;
+ }
+ console.log(f);
+ fs.readFile(f, function(err, data){
+ response.end(f);
+ });
+}).listen(8080);
View
13 leanNodeBy50Examp/chapter_02/1.HandlingPostData/form.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title></title>
+</head>
+<body>
+ <form method="post">
+ <input type="text" name="userinput1"><br>
+ <input type="text" name="userinput2"><br>
+ <input type="submit">
+ </form>
+</body>
+</html>
View
32 leanNodeBy50Examp/chapter_02/1.HandlingPostData/server.js
@@ -0,0 +1,32 @@
+var http = require('http');
+var querystring = require('querystring');
+var util = require('util');
+var form = require('fs').readFileSync('form.html');
+
+var maxData = 2 * 1024 * 1024;
+
+http.createServer(function(request, response){
+ if (request.method === 'GET'){
+ response.writeHead(200, {'Content-Type':'text/html'});
+ response.end(form);
+ }
+ if (request.method = 'POST'){
+ var postData = '';
+ request.on('data', function(chuck){
+ postData += chuck;
+ console.log('postDasta.length: ' + postData.length);
+ if (postData.length > maxData){
+ postData = '';
+ this.pause();
+ response.writeHead(413);
+ response.end('too large');
+ }
+ }).on('end', function(){
+ if (!postData){
+ response.end();
+ }
+ var postDataObject = querystring.parse(postData);
+ response.end('you posted:\n' + util.inspect(postDataObject));
+ });
+ }
+}).listen(8080);
View
13 leanNodeBy50Examp/chapter_02/2.UsingConnect/form.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html>
+<head>
+ <title></title>
+</head>
+<body>
+ <form method="post">
+ <input type="text" name="userinput1"><br>
+ <input type="text" name="userinput2"><br>
+ <input type="submit">
+ </form>
+</body>
+</html>
View
13 leanNodeBy50Examp/chapter_02/2.UsingConnect/server.js
@@ -0,0 +1,13 @@
+var connect = require('connect');
+var util = require('util');
+var form = require('fs').readFileSync('form.html');
+
+connect.createServer(function(request, response){
+ if (request.method === 'POST'){
+ response.end('You posted: \n' + util.inspect(request.body));
+ }
+ if (request.method === 'GET'){
+ response.writeHead(200, {'Content-Type':'text/html'});
+ response.end(form);
+ }
+}).listen(8080);
Please sign in to comment.
Something went wrong with that request. Please try again.