rabies 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,117 @@
1
+ #include "ruby.h"
2
+ #include "intern.h"
3
+ #include "ctype.h"
4
+ #include "cgi.h"
5
+
6
+ void Init_CGI() {
7
+ VALUE cgi = rb_define_class("CGI", rb_cObject);
8
+
9
+ rb_define_module_function(cgi, "escape", CGI_escape, 1);
10
+ rb_define_module_function(cgi, "escape_without_plus", CGI_escape_without_plus, 1);
11
+
12
+ rb_define_module_function(cgi, "unescape", CGI_unescape, 1);
13
+
14
+ rb_define_module_function(cgi, "escapeHTML", CGI_escapeHTML, 1);
15
+ }
16
+
17
+ inline char digit2hex(char ch) {
18
+ if (ch < 9) return '0' + ch;
19
+ else return 'A' + ch - 10;
20
+ }
21
+ inline char hex2digit(char ch) {
22
+ if (isdigit(ch)) return ch - '0';
23
+ else if (ch >= 'A' && ch <= 'F') return ch - 'A';
24
+ else if (ch >= 'a' && ch <= 'f') return ch - 'a';
25
+ else return 0;
26
+ }
27
+
28
+ VALUE CGI_escape(VALUE cgi, VALUE str) {
29
+ return CGI_internal_escape(cgi, str, 1);
30
+ }
31
+ VALUE CGI_escape_without_plus(VALUE cgi, VALUE str) {
32
+ return CGI_internal_escape(cgi, str, 0);
33
+ }
34
+
35
+ VALUE CGI_internal_escape(VALUE cgi, VALUE str, int use_pluses) {
36
+ long output_length = 1;
37
+ char *input, *output, *r, *w, ch;
38
+
39
+ r = input = StringValueCStr(str);
40
+ // First find the length of the resulting string we'll need.
41
+ while ((ch = *r)) {
42
+ ++r;
43
+ if (isalpha(ch) || isdigit(ch) || (ch == ' ' && use_pluses) || ch == '_' || ch == '.' || ch == '-')
44
+ output_length += 1; // Normal takes 1 character
45
+ else
46
+ output_length += 3; // Escaped, needs %XX characters
47
+ }
48
+
49
+ // Create the string of proper size
50
+ w = output = ALLOC_N(char, output_length);
51
+ r = input;
52
+
53
+ // And fill it with the escaped characters
54
+ while ((ch = *r)) {
55
+ r++;
56
+ if (isalpha(ch) || isdigit(ch) || ch == '_' || ch == '.' || ch == '-')
57
+ *w++ = ch;
58
+ else if (ch == ' ' && use_pluses)
59
+ *w++ = '+';
60
+ else {
61
+ *w++ = '%';
62
+ *w++ = digit2hex((ch / 0x10) & 0xF);
63
+ *w++ = digit2hex(ch & 0xF);
64
+ }
65
+ }
66
+ *w = '\0';
67
+ return rb_str_new2(output);
68
+ }
69
+
70
+ VALUE CGI_unescape(VALUE cgi, VALUE str) {
71
+ char *input, *output, *r, *w, ch;
72
+ r = input = StringValueCStr(str);
73
+ w = output = ALLOC_N(char, RSTRING(str)->len + 1);
74
+
75
+ while ((ch = *r)) {
76
+ ++r;
77
+ if (ch == '%')
78
+ *w++ = (hex2digit(*r++) * 0x10) | hex2digit(*r++);
79
+ else if (ch == '+') *w++ = ' ';
80
+ else *w++ = ch;
81
+ }
82
+ *w = '\0';
83
+
84
+ return rb_str_new2(output);
85
+ }
86
+
87
+ #define CH(c) (*w++ = c)
88
+ VALUE CGI_escapeHTML(VALUE cgi, VALUE str) {
89
+ long output_length = 1;
90
+ char *input, *output, *r, *w, ch;
91
+
92
+ r = input = StringValueCStr(str);
93
+ // First find the length of the resulting string we'll need.
94
+ while ((ch = *r)) {
95
+ ++r;
96
+ if (ch == '&') output_length += 5;
97
+ else if (ch == '"') output_length += 6;
98
+ else if (ch == '<' || ch == '>') output_length += 4;
99
+ else output_length++;
100
+ }
101
+
102
+ // Create the string of proper size
103
+ w = output = ALLOC_N(char, output_length);
104
+ r = input;
105
+
106
+ // And fill it with the escaped characters
107
+ while ((ch = *r)) {
108
+ r++;
109
+ if (ch == '&') { CH('&'); CH('a'); CH('m'); CH('p'); CH(';'); }
110
+ else if (ch == '"') { CH('&'); CH('q'); CH('u'); CH('o'); CH('t'); CH(';'); }
111
+ else if (ch == '<' || ch == '>') { CH('&'); CH(ch == '>' ? 'g' : 'l'); CH(';'); }
112
+ else CH(ch);
113
+ }
114
+ *w = '\0';
115
+
116
+ return rb_str_new2(output);
117
+ }
@@ -0,0 +1,11 @@
1
+ #include "ruby.h"
2
+
3
+ void Init_CGI();
4
+
5
+ VALUE CGI_escape(VALUE cgi, VALUE str);
6
+ VALUE CGI_escape_without_plus(VALUE cgi, VALUE str);
7
+ VALUE CGI_internal_escape(VALUE cgi, VALUE str, int use_pluses);
8
+
9
+ VALUE CGI_unescape(VALUE cgi, VALUE str);
10
+
11
+ VALUE CGI_escapeHTML(VALUE cgi, VALUE str);
@@ -0,0 +1,2 @@
1
+ require 'mkmf'
2
+ create_makefile('rabies')
@@ -0,0 +1,9 @@
1
+ #include "ruby.h"
2
+
3
+ #include "cgi.h"
4
+ #include "uri.h"
5
+
6
+ void Init_rabies() {
7
+ Init_CGI();
8
+ Init_URI();
9
+ }
@@ -0,0 +1,7 @@
1
+ #include "uri.h"
2
+
3
+ void Init_URI() {
4
+ VALUE uri = rb_define_module("URI");
5
+
6
+ rb_define_module_function(uri, "encode", CGI_escape_without_plus, 1);
7
+ }
@@ -0,0 +1,4 @@
1
+ #include "ruby.h"
2
+ #include "cgi.h"
3
+
4
+ void Init_URI();
@@ -0,0 +1,20 @@
1
+ require 'test/unit'
2
+ require File.dirname(__FILE__) + '/preserve_versions.rb'
3
+
4
+ class << Test::Unit::TestCase
5
+
6
+ def preserve_result_of(target, selector, *arguments)
7
+
8
+ # Find an unused selector for the test case
9
+ s = "test_preservation_of_#{target}_#{selector}"
10
+ i = 1
11
+ i += 1 while instance_methods.include?(s + i.to_s)
12
+
13
+ define_method s + i.to_s do
14
+ old = target.send "old_#{selector}", *arguments
15
+ current = target.send selector, *arguments
16
+ assert_equal old, current, "The result of #{target}.#{selector}(#{arguments.inspect[1..-2]}) changed"
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,15 @@
1
+
2
+ class CGITest < Test::Unit::TestCase
3
+
4
+ preserve_result_of CGI, :escape, "Hello World!"
5
+
6
+ preserve_result_of CGI, :unescape, "Hello+World%21"
7
+ preserve_result_of CGI, :unescape, "Hello%20World%21"
8
+
9
+ preserve_result_of CGI, :escapeHTML, "<script>alert('pwned');</script>"
10
+ preserve_result_of CGI, :escapeHTML, "<b>All your base<b><pwned>are belong to us</pwned>"
11
+
12
+ # This seems broken, but we'll preserve the behavior for now
13
+ preserve_result_of CGI, :escapeHTML, "<b>I have π for dinner…</b>"
14
+
15
+ end
@@ -0,0 +1,19 @@
1
+
2
+ class << Module
3
+
4
+ def preserve_method(*selectors)
5
+ selectors.flatten.each { |sel| alias_method "old_#{sel}", sel }
6
+ end
7
+
8
+ end
9
+
10
+ require 'cgi'
11
+ class << CGI; preserve_method %w(escape unescape escapeHTML); end
12
+
13
+ require 'uri'
14
+ class << URI; preserve_method %w(encode); end
15
+
16
+
17
+ # Now require rabies:
18
+ $: << File.expand_path(File.dirname(__FILE__) + '/../src/')
19
+ require 'rabies'
@@ -0,0 +1,6 @@
1
+
2
+ class URITest < Test::Unit::TestCase
3
+
4
+ preserve_result_of URI, :encode, "Hello World!"
5
+
6
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.0
3
+ specification_version: 1
4
+ name: rabies
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.0.1
7
+ date: 2007-03-12 00:00:00 -04:00
8
+ summary: A library which replaces hotspots in Rails with C implementations to increase performance.
9
+ require_paths:
10
+ - lib
11
+ email: nseckar@gmail.com
12
+ homepage: http://http://code.google.com/p/rabies/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire: rabies
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - Nicholas Seckar
31
+ files:
32
+ - src/cgi.c
33
+ - src/cgi.h
34
+ - src/extconf.rb
35
+ - src/rabies.c
36
+ - src/uri.c
37
+ - src/uri.h
38
+ - test/abstract.rb
39
+ - test/cgi_test.rb
40
+ - test/preserve_versions.rb
41
+ - test/uri_test.rb
42
+ test_files:
43
+ - test/cgi_test.rb
44
+ - test/uri_test.rb
45
+ rdoc_options: []
46
+
47
+ extra_rdoc_files: []
48
+
49
+ executables: []
50
+
51
+ extensions:
52
+ - src/extconf.rb
53
+ requirements: []
54
+
55
+ dependencies: []
56
+