woolen_common 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/README.md +31 -0
- data/ext/woolen_common/extconf.rb +26 -0
- data/ext/woolen_common/linux.h +3 -0
- data/ext/woolen_common/win.c +18 -0
- data/ext/woolen_common/win.h +4 -0
- data/ext/woolen_common/win/puts_color.c +139 -0
- data/ext/woolen_common/win/puts_color.h +5 -0
- data/ext/woolen_common/woolen_common.c +20 -0
- data/ext/woolen_common/woolen_common.h +7 -0
- data/lib/woolen_common.rb +39 -0
- data/lib/woolen_common/abstract_middleware/builder.rb +138 -0
- data/lib/woolen_common/abstract_middleware/map_cfg_manager.rb +52 -0
- data/lib/woolen_common/abstract_middleware/runner.rb +72 -0
- data/lib/woolen_common/action_pool_proxy.rb +28 -0
- data/lib/woolen_common/actionpool.rb +10 -0
- data/lib/woolen_common/actionpool/pool.rb +295 -0
- data/lib/woolen_common/actionpool/queue.rb +41 -0
- data/lib/woolen_common/actionpool/thread.rb +181 -0
- data/lib/woolen_common/addr_helper.rb +93 -0
- data/lib/woolen_common/cache.rb +305 -0
- data/lib/woolen_common/common_helper.rb +42 -0
- data/lib/woolen_common/config_manager.rb +36 -0
- data/lib/woolen_common/drb_helper.rb +125 -0
- data/lib/woolen_common/ffi/win32_kernel32.rb +86 -0
- data/lib/woolen_common/logger.rb +419 -0
- data/lib/woolen_common/pcap/mu/fixnum_ext.rb +8 -0
- data/lib/woolen_common/pcap/mu/pcap/ethernet.rb +164 -0
- data/lib/woolen_common/pcap/mu/pcap/header.rb +76 -0
- data/lib/woolen_common/pcap/mu/pcap/io_pair.rb +68 -0
- data/lib/woolen_common/pcap/mu/pcap/io_wrapper.rb +77 -0
- data/lib/woolen_common/pcap/mu/pcap/ip.rb +62 -0
- data/lib/woolen_common/pcap/mu/pcap/ipv4.rb +274 -0
- data/lib/woolen_common/pcap/mu/pcap/ipv6.rb +149 -0
- data/lib/woolen_common/pcap/mu/pcap/packet.rb +106 -0
- data/lib/woolen_common/pcap/mu/pcap/pkthdr.rb +162 -0
- data/lib/woolen_common/pcap/mu/pcap/reader.rb +62 -0
- data/lib/woolen_common/pcap/mu/pcap/reader/http_family.rb +175 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp.rb +369 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk.rb +124 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/data.rb +135 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init.rb +101 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/chunk/init_ack.rb +69 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/parameter.rb +111 -0
- data/lib/woolen_common/pcap/mu/pcap/sctp/parameter/ip_address.rb +49 -0
- data/lib/woolen_common/pcap/mu/pcap/stream_packetizer.rb +74 -0
- data/lib/woolen_common/pcap/mu/pcap/tcp.rb +522 -0
- data/lib/woolen_common/pcap/mu/pcap/udp.rb +81 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap.rb +175 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap/fields.rb +51 -0
- data/lib/woolen_common/pcap/mu/scenario/pcap/rtp.rb +72 -0
- data/lib/woolen_common/pcap/pcap.rb +115 -0
- data/lib/woolen_common/pcap/readme.md +72 -0
- data/lib/woolen_common/ruby_ext/blank.rb +126 -0
- data/lib/woolen_common/ruby_ext/drb_ext.rb +7 -0
- data/lib/woolen_common/ruby_ext/string.rb +116 -0
- data/lib/woolen_common/ruby_ext/win32_ole.rb +4 -0
- data/lib/woolen_common/ruby_proxy.rb +5 -0
- data/lib/woolen_common/ruby_proxy/client.rb +305 -0
- data/lib/woolen_common/ruby_proxy/config.rb +44 -0
- data/lib/woolen_common/ruby_proxy/exceptions.rb +17 -0
- data/lib/woolen_common/ruby_proxy/proxy.rb +157 -0
- data/lib/woolen_common/ruby_proxy/proxy_global_set.rb +44 -0
- data/lib/woolen_common/ruby_proxy/proxy_load.rb +34 -0
- data/lib/woolen_common/ruby_proxy/server.rb +53 -0
- data/lib/woolen_common/splib.rb +36 -0
- data/lib/woolen_common/splib/Array.rb +33 -0
- data/lib/woolen_common/splib/CodeReloader.rb +59 -0
- data/lib/woolen_common/splib/Constants.rb +44 -0
- data/lib/woolen_common/splib/Conversions.rb +47 -0
- data/lib/woolen_common/splib/Exec.rb +132 -0
- data/lib/woolen_common/splib/Float.rb +13 -0
- data/lib/woolen_common/splib/HumanIdealRandomIterator.rb +40 -0
- data/lib/woolen_common/splib/Monitor.rb +214 -0
- data/lib/woolen_common/splib/PriorityQueue.rb +110 -0
- data/lib/woolen_common/splib/Sleep.rb +10 -0
- data/lib/woolen_common/splib/UrlShorteners.rb +48 -0
- data/lib/woolen_common/ssh_proxy.rb +146 -0
- data/lib/woolen_common/system_helper.rb +123 -0
- data/lib/woolen_common/system_monitor.rb +23 -0
- data/lib/woolen_common/system_monitor/linux_monitor.rb +250 -0
- data/lib/woolen_common/system_monitor/windows_monitor.rb +145 -0
- data/lib/woolen_common/type_helper.rb +42 -0
- data/lib/woolen_common/ver_ctrl_middle_ware.rb +92 -0
- data/lib/woolen_common/version.rb +3 -0
- metadata +210 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YWFmY2FmOWMwMTMyYWEyM2M4N2IxZDQ4NzFmYjQ5ZDNmZDQ0NmI5MA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MTgyYjM5NzE5Y2I0OTA5MTg1ZWQyYjc2NzZiNmZkZjdlN2EyNGQzYw==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OWFjNGVhNTRhM2Y0NTExMDMyZDNjMTQ5YTgzOGM5Y2E0ZWM5ZWQwMzk1YjJh
|
10
|
+
Yjc2OTY1M2JmM2U4Y2RjZWMyNDM3MTk5M2ZmOTQwYzRhMTVjOTU2ZjgxMGJm
|
11
|
+
MmFjNGJlMDhiY2M4ZTE2ODc3OWU3MzFlYjlhNWRmN2MyNTZiMTE=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZGM5ZmUzNGUyZWRkNTZmNjE1ZDA5ZGI2YTQxYzk5N2U5M2FmZGQ1ZDUzMTJm
|
14
|
+
OGY0ZGE1OWRmN2MwNTFkN2NjMzczYmQ5OWIyNDRiNTgyMWM4ODdmZjMzODg5
|
15
|
+
MzJiMDgwMGI3ZGQ3MjM4OWE0ZWExNTY0MTJlMDBiZjlmNzk5ZGM=
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# WoolenCommon
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'woolen_common'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install woolen_common
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/woolen_common/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
CC = RbConfig::CONFIG['CC']
|
4
|
+
if CC =~ /clang/
|
5
|
+
RbConfig::MAKEFILE_CONFIG['try_header'] = :try_cpp
|
6
|
+
RbConfig::CONFIG['CPP'] = "#{CC} -E"
|
7
|
+
elsif RbConfig::CONFIG['arch'] =~ /mswin32|mingw/
|
8
|
+
RbConfig::MAKEFILE_CONFIG['try_header'] = :try_cpp
|
9
|
+
RbConfig::CONFIG['CPP'] = "#{CC} /P"
|
10
|
+
end
|
11
|
+
|
12
|
+
require "mkmf"
|
13
|
+
extention_name = 'woolen_common'
|
14
|
+
|
15
|
+
if $warnflags
|
16
|
+
$warnflags.slice!('-Wdeclaration-after-statement')
|
17
|
+
$warnflags.slice!('-Wimplicit-function-declaration')
|
18
|
+
end
|
19
|
+
|
20
|
+
# Quick fix for 1.8.7
|
21
|
+
$CFLAGS << " -I#{File.dirname(__FILE__)}/ext/woolen_common"
|
22
|
+
$CPPFLAGS << " -I#{File.dirname(__FILE__)}/ext/woolen_common"
|
23
|
+
|
24
|
+
# Create Makefile
|
25
|
+
create_makefile('woolen_common')
|
26
|
+
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#ifdef _WIN32
|
2
|
+
#include "ruby.h"
|
3
|
+
#include "win/puts_color.c"
|
4
|
+
#include "woolen_common.h"
|
5
|
+
static VALUE puts_color_rb(VALUE self, VALUE front, VALUE back, VALUE msg)
|
6
|
+
{
|
7
|
+
char * front_c = StringValueCStr(front);
|
8
|
+
char * back_c = StringValueCStr(back);
|
9
|
+
char * msg_c = StringValueCStr(msg);
|
10
|
+
puts_color(front_c,back_c,msg_c);
|
11
|
+
return 0;
|
12
|
+
}
|
13
|
+
|
14
|
+
void setup_win_method()
|
15
|
+
{
|
16
|
+
rb_define_method(my_logger_rb,"c_puts_color",puts_color_rb,3);
|
17
|
+
}
|
18
|
+
#endif
|
@@ -0,0 +1,139 @@
|
|
1
|
+
#include <windows.h>
|
2
|
+
#include <string.h>
|
3
|
+
#include <assert.h>
|
4
|
+
#include <stdio.h>
|
5
|
+
|
6
|
+
#define BACK 0
|
7
|
+
#define FORE 1
|
8
|
+
#define COLOR_COUNT 9
|
9
|
+
|
10
|
+
//支持的颜色选项
|
11
|
+
char g_colors_name[17][20] =
|
12
|
+
{
|
13
|
+
"default", "black", "navy", "green",
|
14
|
+
"teal", "maroon", "purple", "olive",
|
15
|
+
"silver", "gray", "blue", "lime",
|
16
|
+
"aqua", "red", "fuchsia", "yellow",
|
17
|
+
"white"
|
18
|
+
};
|
19
|
+
//颜色排列的系统值组合
|
20
|
+
int g_front_colors_id[COLOR_COUNT] =
|
21
|
+
{
|
22
|
+
-1,0,
|
23
|
+
FOREGROUND_BLUE , FOREGROUND_GREEN ,FOREGROUND_BLUE | FOREGROUND_GREEN,
|
24
|
+
FOREGROUND_RED, FOREGROUND_BLUE | FOREGROUND_RED,FOREGROUND_RED | FOREGROUND_GREEN,
|
25
|
+
FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED
|
26
|
+
};
|
27
|
+
|
28
|
+
int g_back_colors_id[COLOR_COUNT] =
|
29
|
+
{
|
30
|
+
-1,0,
|
31
|
+
BACKGROUND_BLUE , BACKGROUND_GREEN ,BACKGROUND_BLUE | BACKGROUND_GREEN,
|
32
|
+
BACKGROUND_RED, BACKGROUND_BLUE | BACKGROUND_RED,BACKGROUND_RED | BACKGROUND_GREEN,
|
33
|
+
BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED
|
34
|
+
};
|
35
|
+
// 根据side确定是取背景还是字体的颜色
|
36
|
+
// color是一个字符串,定位颜色
|
37
|
+
int get_one_color_mod(const char * color,int side)
|
38
|
+
{
|
39
|
+
int count = 0;
|
40
|
+
for(count =0; count<17; count++)
|
41
|
+
{
|
42
|
+
if(stricmp(color,g_colors_name[count]) == 0)
|
43
|
+
{
|
44
|
+
if(count >= COLOR_COUNT)
|
45
|
+
{
|
46
|
+
int result_count = count +1 ;
|
47
|
+
if (side == BACK)
|
48
|
+
{
|
49
|
+
return g_back_colors_id[result_count % COLOR_COUNT] | BACKGROUND_INTENSITY;
|
50
|
+
}
|
51
|
+
else
|
52
|
+
{
|
53
|
+
return g_front_colors_id[result_count % COLOR_COUNT] | FOREGROUND_INTENSITY;
|
54
|
+
}
|
55
|
+
}
|
56
|
+
else
|
57
|
+
{
|
58
|
+
if (side == BACK)
|
59
|
+
{
|
60
|
+
return g_back_colors_id[count % COLOR_COUNT] ;
|
61
|
+
}
|
62
|
+
else
|
63
|
+
{
|
64
|
+
return g_front_colors_id[count % COLOR_COUNT] ;
|
65
|
+
}
|
66
|
+
}
|
67
|
+
}
|
68
|
+
}
|
69
|
+
int num = atoi(color);
|
70
|
+
if(num >= COLOR_COUNT)
|
71
|
+
{
|
72
|
+
int result_count = num +1 ;
|
73
|
+
if (side == BACK)
|
74
|
+
{
|
75
|
+
return g_back_colors_id[result_count % COLOR_COUNT] | BACKGROUND_INTENSITY ;
|
76
|
+
}
|
77
|
+
else
|
78
|
+
{
|
79
|
+
return g_front_colors_id[result_count % COLOR_COUNT] | FOREGROUND_INTENSITY;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
else
|
83
|
+
{
|
84
|
+
if (side == BACK)
|
85
|
+
{
|
86
|
+
return g_back_colors_id[num % COLOR_COUNT] ;
|
87
|
+
}
|
88
|
+
else
|
89
|
+
{
|
90
|
+
return g_front_colors_id[num % COLOR_COUNT] ;
|
91
|
+
}
|
92
|
+
}
|
93
|
+
}
|
94
|
+
|
95
|
+
int get_color_mod(const char * front_color, const char * back_color)
|
96
|
+
{
|
97
|
+
assert(front_color != NULL);
|
98
|
+
assert(back_color != NULL);
|
99
|
+
assert(strlen(front_color) != 0);
|
100
|
+
assert(strlen(back_color) != 0);
|
101
|
+
//cout << front_color << endl;
|
102
|
+
//cout << back_color << endl;
|
103
|
+
int front_color_mod = 0;
|
104
|
+
int back_color_mod = 0;
|
105
|
+
// 根据不同字符串,去获取颜色值,其中字体值和背景值根据第二个参数确定的
|
106
|
+
front_color_mod = get_one_color_mod(front_color,FORE);
|
107
|
+
back_color_mod = get_one_color_mod(back_color,BACK);
|
108
|
+
if(front_color_mod == -1)
|
109
|
+
{
|
110
|
+
return back_color_mod;
|
111
|
+
}
|
112
|
+
if(back_color_mod == -1)
|
113
|
+
{
|
114
|
+
return front_color_mod;
|
115
|
+
}
|
116
|
+
return back_color_mod | front_color_mod;
|
117
|
+
}
|
118
|
+
//重置颜色值,这里还原环境,假设默认环境是白字黑底的
|
119
|
+
void reset_color()
|
120
|
+
{
|
121
|
+
HANDLE hOut = NULL;
|
122
|
+
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
123
|
+
int mod_mask = 0x00ff,color_mod = 0;
|
124
|
+
color_mod = get_color_mod("silver","black");
|
125
|
+
SetConsoleTextAttribute(hOut,mod_mask & color_mod);
|
126
|
+
//printf("");
|
127
|
+
}
|
128
|
+
int puts_color(const char * front, const char * back, const char * msg)
|
129
|
+
{
|
130
|
+
// HANDLE hOut;
|
131
|
+
int mod_mask = 0x00ff;
|
132
|
+
int color_mod = 0;
|
133
|
+
color_mod = get_color_mod(front,back);
|
134
|
+
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
|
135
|
+
SetConsoleTextAttribute(hOut,mod_mask & color_mod);
|
136
|
+
printf(msg);
|
137
|
+
reset_color();
|
138
|
+
return 0;
|
139
|
+
}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
VALUE woolen_common_module_rb = 0;
|
3
|
+
VALUE my_logger_rb = 0;
|
4
|
+
|
5
|
+
#ifdef _WIN32
|
6
|
+
#include "win.h"
|
7
|
+
#endif
|
8
|
+
|
9
|
+
#ifdef __linux__
|
10
|
+
#include "linux.h"
|
11
|
+
#endif
|
12
|
+
|
13
|
+
void Init_woolen_common()
|
14
|
+
{
|
15
|
+
woolen_common_module_rb = rb_define_module("WoolenCommon");
|
16
|
+
my_logger_rb = rb_define_class_under(woolen_common_module_rb,"MyLogger",rb_cObject);
|
17
|
+
#ifdef _WIN32
|
18
|
+
setup_win_method();
|
19
|
+
#endif
|
20
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'version')}"
|
3
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'actionpool')}"
|
4
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'config_manager')}"
|
5
|
+
|
6
|
+
module WoolenCommon
|
7
|
+
|
8
|
+
def setup(prj_root=nil)
|
9
|
+
unless prj_root
|
10
|
+
prj_root = File.dirname caller[0].split(':')[0]
|
11
|
+
end
|
12
|
+
ConfigManager.project_root = prj_root
|
13
|
+
puts "the prj_root:#{prj_root}"
|
14
|
+
load_all_file
|
15
|
+
end
|
16
|
+
|
17
|
+
def load_all_file
|
18
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'ruby_ext', 'string')}"
|
19
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'ruby_ext', 'blank')}"
|
20
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'ruby_ext', 'drb_ext')}"
|
21
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'ruby_ext', 'win32_ole')}"
|
22
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'common_helper')}"
|
23
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'addr_helper')}"
|
24
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'type_helper')}"
|
25
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'logger')}"
|
26
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'system_helper')}"
|
27
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'system_monitor')}"
|
28
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'splib')}"
|
29
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'action_pool_proxy')}"
|
30
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'ssh_proxy')}"
|
31
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'drb_helper')}"
|
32
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'pcap', 'pcap')}"
|
33
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'ver_ctrl_middle_ware')}"
|
34
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'ruby_proxy')}"
|
35
|
+
require "#{File.join(File.dirname(__FILE__), 'woolen_common', 'cache')}"
|
36
|
+
end
|
37
|
+
|
38
|
+
module_function :setup,:load_all_file
|
39
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module WoolenCommon
|
3
|
+
module Middleware
|
4
|
+
# This provides a DSL for building up a stack of middlewares.
|
5
|
+
#
|
6
|
+
# This code is based heavily off of `Rack::Builder` and
|
7
|
+
# `ActionDispatch::MiddlewareStack` in Rack and Rails, respectively.
|
8
|
+
#
|
9
|
+
# # Usage
|
10
|
+
#
|
11
|
+
# Building a middleware stack is very easy:
|
12
|
+
#
|
13
|
+
# app = Middleware::Builder.new do
|
14
|
+
# use A
|
15
|
+
# use B
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# # Call the middleware
|
19
|
+
# app.call(7)
|
20
|
+
#
|
21
|
+
class Builder
|
22
|
+
# Initializes the builder. An optional block can be passed which
|
23
|
+
# will be evaluated in the context of the instance.
|
24
|
+
#
|
25
|
+
# Example:
|
26
|
+
#
|
27
|
+
# Builder.new do
|
28
|
+
# use A
|
29
|
+
# use B
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
# @param [Hash] opts Options hash
|
33
|
+
# @option opts [Class] :runner_class The class to wrap the middleware stack
|
34
|
+
# in which knows how to run them.
|
35
|
+
# @yield [] Evaluated in this instance which allows you to use methods
|
36
|
+
# like {#use} and such.
|
37
|
+
def initialize(opts=nil, &block)
|
38
|
+
opts ||= {}
|
39
|
+
@runner_class = opts[:runner_class] || Runner
|
40
|
+
instance_eval(&block) if block_given?
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns a mergeable version of the builder. If `use` is called with
|
44
|
+
# the return value of this method, then the stack will merge, instead
|
45
|
+
# of being treated as a separate single middleware.
|
46
|
+
def flatten
|
47
|
+
lambda do |env|
|
48
|
+
self.call(env)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
# Adds a middleware class to the middleware stack. Any additional
|
53
|
+
# args and a block, if given, are saved and passed to the initializer
|
54
|
+
# of the middleware.
|
55
|
+
#
|
56
|
+
# @param [Class] middleware The middleware class
|
57
|
+
def use(middleware, *args, &block)
|
58
|
+
if middleware.kind_of?(Builder)
|
59
|
+
# Merge in the other builder's stack into our own
|
60
|
+
self.stack.concat(middleware.stack)
|
61
|
+
else
|
62
|
+
self.stack << [middleware, args, block]
|
63
|
+
end
|
64
|
+
|
65
|
+
self
|
66
|
+
end
|
67
|
+
|
68
|
+
# Inserts a middleware at the given index or directly before the
|
69
|
+
# given middleware object.
|
70
|
+
def insert(index, middleware, *args, &block)
|
71
|
+
index = self.index(index) unless index.is_a?(Integer)
|
72
|
+
raise "no such middleware to insert before: #{index.inspect}" unless index
|
73
|
+
stack.insert(index, [middleware, args, block])
|
74
|
+
end
|
75
|
+
|
76
|
+
alias_method :insert_before, :insert
|
77
|
+
|
78
|
+
# Inserts a middleware after the given index or middleware object.
|
79
|
+
def insert_after(index, middleware, *args, &block)
|
80
|
+
index = self.index(index) unless index.is_a?(Integer)
|
81
|
+
raise "no such middleware to insert after: #{index.inspect}" unless index
|
82
|
+
insert(index + 1, middleware, *args, &block)
|
83
|
+
end
|
84
|
+
|
85
|
+
# Replaces the given middleware object or index with the new
|
86
|
+
# middleware.
|
87
|
+
def replace(index, middleware, *args, &block)
|
88
|
+
if index.is_a?(Integer)
|
89
|
+
delete(index)
|
90
|
+
insert(index, middleware, *args, &block)
|
91
|
+
else
|
92
|
+
insert_before(index, middleware, *args, &block)
|
93
|
+
delete(index)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
# Deletes the given middleware object or index
|
98
|
+
def delete(index)
|
99
|
+
index = self.index(index) unless index.is_a?(Integer)
|
100
|
+
stack.delete_at(index)
|
101
|
+
end
|
102
|
+
|
103
|
+
# Runs the builder stack with the given environment.
|
104
|
+
def call(env=nil)
|
105
|
+
to_app.call(env)
|
106
|
+
end
|
107
|
+
|
108
|
+
protected
|
109
|
+
|
110
|
+
# Returns the numeric index for the given middleware object.
|
111
|
+
#
|
112
|
+
# @param [Object] object The item to find the index for
|
113
|
+
# @return [Integer]
|
114
|
+
def index(object)
|
115
|
+
stack.each_with_index do |item, i|
|
116
|
+
return i if item[0] == object
|
117
|
+
end
|
118
|
+
|
119
|
+
nil
|
120
|
+
end
|
121
|
+
|
122
|
+
# Returns the current stack of middlewares. You probably won't
|
123
|
+
# need to use this directly, and it's recommended that you don't.
|
124
|
+
#
|
125
|
+
# @return [Array]
|
126
|
+
def stack
|
127
|
+
@stack ||= []
|
128
|
+
end
|
129
|
+
|
130
|
+
# Converts the builder stack to a runnable action sequence.
|
131
|
+
#
|
132
|
+
# @return [Object] A callable object
|
133
|
+
def to_app
|
134
|
+
@runner_class.new(stack.dup)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|