cgi 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of cgi might be problematic. Click here for more details.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.travis.yml +7 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +97 -0
- data/Rakefile +13 -0
- data/bin/console +7 -0
- data/bin/setup +6 -0
- data/cgi.gemspec +25 -0
- data/ext/cgi/escape/depend +17 -0
- data/ext/cgi/escape/escape.c +409 -0
- data/ext/cgi/escape/extconf.rb +3 -0
- data/lib/cgi.rb +296 -0
- data/lib/cgi/cookie.rb +188 -0
- data/lib/cgi/core.rb +888 -0
- data/lib/cgi/html.rb +1035 -0
- data/lib/cgi/session.rb +534 -0
- data/lib/cgi/session/pstore.rb +101 -0
- data/lib/cgi/util.rb +223 -0
- data/lib/cgi/version.rb +3 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cdab469fad9274c1f62d5a4a7d4ffd445bdfb0b596b2abd66e098dffd9e48aab
|
4
|
+
data.tar.gz: 696d10e4d2ecc54f882d190478cac27c5a5b1e879c14eacc0e2ce86b2e00f08a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: edcf932436436eeb2367704011aad3c31220491278bfd6ca06e358799d18b3548fd73cb3714ca84f2cede0aed6d4161b5072c287db98f960ada0c3c96b0247d7
|
7
|
+
data.tar.gz: 96eee5f302ebed6ce10b262b394474d3e96712ae7f6ebc5699cb6c17485d0685aa598aaf9fdbce9b6f46df1360bf4ecb9277d082f1c4a858dd884b6aee5852a9
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.
|
2
|
+
|
3
|
+
Redistribution and use in source and binary forms, with or without
|
4
|
+
modification, are permitted provided that the following conditions
|
5
|
+
are met:
|
6
|
+
1. Redistributions of source code must retain the above copyright
|
7
|
+
notice, this list of conditions and the following disclaimer.
|
8
|
+
2. Redistributions in binary form must reproduce the above copyright
|
9
|
+
notice, this list of conditions and the following disclaimer in the
|
10
|
+
documentation and/or other materials provided with the distribution.
|
11
|
+
|
12
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
13
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
14
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
15
|
+
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
16
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
17
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
18
|
+
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
19
|
+
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
20
|
+
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
21
|
+
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
22
|
+
SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
## Introduction
|
2
|
+
|
3
|
+
CGI is a large class, providing several categories of methods, many of which
|
4
|
+
are mixed in from other modules. Some of the documentation is in this class,
|
5
|
+
some in the modules CGI::QueryExtension and CGI::HtmlExtension. See
|
6
|
+
CGI::Cookie for specific information on handling cookies, and cgi/session.rb
|
7
|
+
(CGI::Session) for information on sessions.
|
8
|
+
|
9
|
+
For queries, CGI provides methods to get at environmental variables,
|
10
|
+
parameters, cookies, and multipart request data. For responses, CGI provides
|
11
|
+
methods for writing output and generating HTML.
|
12
|
+
|
13
|
+
Read on for more details. Examples are provided at the bottom.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'cgi'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install cgi
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
### Get form values
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require "cgi"
|
37
|
+
cgi = CGI.new
|
38
|
+
value = cgi['field_name'] # <== value string for 'field_name'
|
39
|
+
# if not 'field_name' included, then return "".
|
40
|
+
fields = cgi.keys # <== array of field names
|
41
|
+
|
42
|
+
# returns true if form has 'field_name'
|
43
|
+
cgi.has_key?('field_name')
|
44
|
+
cgi.has_key?('field_name')
|
45
|
+
cgi.include?('field_name')
|
46
|
+
```
|
47
|
+
|
48
|
+
CAUTION! cgi['field_name'] returned an Array with the old
|
49
|
+
cgi.rb(included in Ruby 1.6)
|
50
|
+
|
51
|
+
### Get form values as hash
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
require "cgi"
|
55
|
+
cgi = CGI.new
|
56
|
+
params = cgi.params
|
57
|
+
```
|
58
|
+
|
59
|
+
cgi.params is a hash.
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
cgi.params['new_field_name'] = ["value"] # add new param
|
63
|
+
cgi.params['field_name'] = ["new_value"] # change value
|
64
|
+
cgi.params.delete('field_name') # delete param
|
65
|
+
cgi.params.clear # delete all params
|
66
|
+
```
|
67
|
+
|
68
|
+
### Save form values to file
|
69
|
+
|
70
|
+
```ruby
|
71
|
+
require "pstore"
|
72
|
+
db = PStore.new("query.db")
|
73
|
+
db.transaction do
|
74
|
+
db["params"] = cgi.params
|
75
|
+
end
|
76
|
+
```
|
77
|
+
|
78
|
+
|
79
|
+
### Restore form values from file
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
require "pstore"
|
83
|
+
db = PStore.new("query.db")
|
84
|
+
db.transaction do
|
85
|
+
cgi.params = db["params"]
|
86
|
+
end
|
87
|
+
```
|
88
|
+
|
89
|
+
## Development
|
90
|
+
|
91
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
92
|
+
|
93
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
94
|
+
|
95
|
+
## Contributing
|
96
|
+
|
97
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/cgi.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test/lib"
|
6
|
+
t.ruby_opts << "-rhelper"
|
7
|
+
t.test_files = FileList['test/**/test_*.rb']
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rake/extensiontask'
|
11
|
+
Rake::ExtensionTask.new("cgi/escape")
|
12
|
+
|
13
|
+
task :default => :test
|
data/bin/console
ADDED
data/bin/setup
ADDED
data/cgi.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "cgi/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "cgi"
|
7
|
+
spec.version = CGI::VERSION
|
8
|
+
spec.authors = ["Hiroshi SHIBATA"]
|
9
|
+
spec.email = ["hsbt@ruby-lang.org"]
|
10
|
+
|
11
|
+
spec.summary = %q{Support for the Common Gateway Interface protocol.}
|
12
|
+
spec.description = %q{Support for the Common Gateway Interface protocol.}
|
13
|
+
spec.homepage = "https://github.com/ruby/cgi"
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
|
18
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
spec.bindir = "exe"
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ["lib"]
|
24
|
+
spec.license = "BSD-2-Clause"
|
25
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# AUTOGENERATED DEPENDENCIES START
|
2
|
+
escape.o: $(RUBY_EXTCONF_H)
|
3
|
+
escape.o: $(arch_hdrdir)/ruby/config.h
|
4
|
+
escape.o: $(hdrdir)/ruby.h
|
5
|
+
escape.o: $(hdrdir)/ruby/assert.h
|
6
|
+
escape.o: $(hdrdir)/ruby/backward.h
|
7
|
+
escape.o: $(hdrdir)/ruby/defines.h
|
8
|
+
escape.o: $(hdrdir)/ruby/encoding.h
|
9
|
+
escape.o: $(hdrdir)/ruby/intern.h
|
10
|
+
escape.o: $(hdrdir)/ruby/missing.h
|
11
|
+
escape.o: $(hdrdir)/ruby/onigmo.h
|
12
|
+
escape.o: $(hdrdir)/ruby/oniguruma.h
|
13
|
+
escape.o: $(hdrdir)/ruby/ruby.h
|
14
|
+
escape.o: $(hdrdir)/ruby/st.h
|
15
|
+
escape.o: $(hdrdir)/ruby/subst.h
|
16
|
+
escape.o: escape.c
|
17
|
+
# AUTOGENERATED DEPENDENCIES END
|
@@ -0,0 +1,409 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "ruby/encoding.h"
|
3
|
+
|
4
|
+
RUBY_EXTERN unsigned long ruby_scan_digits(const char *str, ssize_t len, int base, size_t *retlen, int *overflow);
|
5
|
+
RUBY_EXTERN const char ruby_hexdigits[];
|
6
|
+
RUBY_EXTERN const signed char ruby_digit36_to_number_table[];
|
7
|
+
#define lower_hexdigits (ruby_hexdigits+0)
|
8
|
+
#define upper_hexdigits (ruby_hexdigits+16)
|
9
|
+
#define char_to_number(c) ruby_digit36_to_number_table[(unsigned char)(c)]
|
10
|
+
|
11
|
+
static VALUE rb_cCGI, rb_mUtil, rb_mEscape;
|
12
|
+
static ID id_accept_charset;
|
13
|
+
|
14
|
+
#define HTML_ESCAPE_MAX_LEN 6
|
15
|
+
|
16
|
+
static const struct {
|
17
|
+
uint8_t len;
|
18
|
+
char str[HTML_ESCAPE_MAX_LEN+1];
|
19
|
+
} html_escape_table[UCHAR_MAX+1] = {
|
20
|
+
#define HTML_ESCAPE(c, str) [c] = {rb_strlen_lit(str), str}
|
21
|
+
HTML_ESCAPE('\'', "'"),
|
22
|
+
HTML_ESCAPE('&', "&"),
|
23
|
+
HTML_ESCAPE('"', """),
|
24
|
+
HTML_ESCAPE('<', "<"),
|
25
|
+
HTML_ESCAPE('>', ">"),
|
26
|
+
#undef HTML_ESCAPE
|
27
|
+
};
|
28
|
+
|
29
|
+
static inline void
|
30
|
+
preserve_original_state(VALUE orig, VALUE dest)
|
31
|
+
{
|
32
|
+
rb_enc_associate(dest, rb_enc_get(orig));
|
33
|
+
|
34
|
+
RB_OBJ_INFECT_RAW(dest, orig);
|
35
|
+
}
|
36
|
+
|
37
|
+
static VALUE
|
38
|
+
optimized_escape_html(VALUE str)
|
39
|
+
{
|
40
|
+
VALUE vbuf;
|
41
|
+
char *buf = ALLOCV_N(char, vbuf, RSTRING_LEN(str) * HTML_ESCAPE_MAX_LEN);
|
42
|
+
const char *cstr = RSTRING_PTR(str);
|
43
|
+
const char *end = cstr + RSTRING_LEN(str);
|
44
|
+
|
45
|
+
char *dest = buf;
|
46
|
+
while (cstr < end) {
|
47
|
+
const unsigned char c = *cstr++;
|
48
|
+
uint8_t len = html_escape_table[c].len;
|
49
|
+
if (len) {
|
50
|
+
memcpy(dest, html_escape_table[c].str, len);
|
51
|
+
dest += len;
|
52
|
+
}
|
53
|
+
else {
|
54
|
+
*dest++ = c;
|
55
|
+
}
|
56
|
+
}
|
57
|
+
|
58
|
+
VALUE escaped;
|
59
|
+
if (RSTRING_LEN(str) < (dest - buf)) {
|
60
|
+
escaped = rb_str_new(buf, dest - buf);
|
61
|
+
preserve_original_state(str, escaped);
|
62
|
+
}
|
63
|
+
else {
|
64
|
+
escaped = rb_str_dup(str);
|
65
|
+
}
|
66
|
+
ALLOCV_END(vbuf);
|
67
|
+
return escaped;
|
68
|
+
}
|
69
|
+
|
70
|
+
static VALUE
|
71
|
+
optimized_unescape_html(VALUE str)
|
72
|
+
{
|
73
|
+
enum {UNICODE_MAX = 0x10ffff};
|
74
|
+
rb_encoding *enc = rb_enc_get(str);
|
75
|
+
unsigned long charlimit = (strcasecmp(rb_enc_name(enc), "UTF-8") == 0 ? UNICODE_MAX :
|
76
|
+
strcasecmp(rb_enc_name(enc), "ISO-8859-1") == 0 ? 256 :
|
77
|
+
128);
|
78
|
+
long i, len, beg = 0;
|
79
|
+
size_t clen, plen;
|
80
|
+
int overflow;
|
81
|
+
const char *cstr;
|
82
|
+
char buf[6];
|
83
|
+
VALUE dest = 0;
|
84
|
+
|
85
|
+
len = RSTRING_LEN(str);
|
86
|
+
cstr = RSTRING_PTR(str);
|
87
|
+
|
88
|
+
for (i = 0; i < len; i++) {
|
89
|
+
unsigned long cc;
|
90
|
+
char c = cstr[i];
|
91
|
+
if (c != '&') continue;
|
92
|
+
plen = i - beg;
|
93
|
+
if (++i >= len) break;
|
94
|
+
c = (unsigned char)cstr[i];
|
95
|
+
#define MATCH(s) (len - i >= (int)rb_strlen_lit(s) && \
|
96
|
+
memcmp(&cstr[i], s, rb_strlen_lit(s)) == 0 && \
|
97
|
+
(i += rb_strlen_lit(s) - 1, 1))
|
98
|
+
switch (c) {
|
99
|
+
case 'a':
|
100
|
+
++i;
|
101
|
+
if (MATCH("pos;")) {
|
102
|
+
c = '\'';
|
103
|
+
}
|
104
|
+
else if (MATCH("mp;")) {
|
105
|
+
c = '&';
|
106
|
+
}
|
107
|
+
else continue;
|
108
|
+
break;
|
109
|
+
case 'q':
|
110
|
+
++i;
|
111
|
+
if (MATCH("uot;")) {
|
112
|
+
c = '"';
|
113
|
+
}
|
114
|
+
else continue;
|
115
|
+
break;
|
116
|
+
case 'g':
|
117
|
+
++i;
|
118
|
+
if (MATCH("t;")) {
|
119
|
+
c = '>';
|
120
|
+
}
|
121
|
+
else continue;
|
122
|
+
break;
|
123
|
+
case 'l':
|
124
|
+
++i;
|
125
|
+
if (MATCH("t;")) {
|
126
|
+
c = '<';
|
127
|
+
}
|
128
|
+
else continue;
|
129
|
+
break;
|
130
|
+
case '#':
|
131
|
+
if (len - ++i >= 2 && ISDIGIT(cstr[i])) {
|
132
|
+
cc = ruby_scan_digits(&cstr[i], len-i, 10, &clen, &overflow);
|
133
|
+
}
|
134
|
+
else if ((cstr[i] == 'x' || cstr[i] == 'X') && len - ++i >= 2 && ISXDIGIT(cstr[i])) {
|
135
|
+
cc = ruby_scan_digits(&cstr[i], len-i, 16, &clen, &overflow);
|
136
|
+
}
|
137
|
+
else continue;
|
138
|
+
i += clen;
|
139
|
+
if (overflow || cc >= charlimit || cstr[i] != ';') continue;
|
140
|
+
if (!dest) {
|
141
|
+
dest = rb_str_buf_new(len);
|
142
|
+
}
|
143
|
+
rb_str_cat(dest, cstr + beg, plen);
|
144
|
+
if (charlimit > 256) {
|
145
|
+
rb_str_cat(dest, buf, rb_enc_mbcput((OnigCodePoint)cc, buf, enc));
|
146
|
+
}
|
147
|
+
else {
|
148
|
+
c = (unsigned char)cc;
|
149
|
+
rb_str_cat(dest, &c, 1);
|
150
|
+
}
|
151
|
+
beg = i + 1;
|
152
|
+
continue;
|
153
|
+
default:
|
154
|
+
--i;
|
155
|
+
continue;
|
156
|
+
}
|
157
|
+
if (!dest) {
|
158
|
+
dest = rb_str_buf_new(len);
|
159
|
+
}
|
160
|
+
rb_str_cat(dest, cstr + beg, plen);
|
161
|
+
rb_str_cat(dest, &c, 1);
|
162
|
+
beg = i + 1;
|
163
|
+
}
|
164
|
+
|
165
|
+
if (dest) {
|
166
|
+
rb_str_cat(dest, cstr + beg, len - beg);
|
167
|
+
preserve_original_state(str, dest);
|
168
|
+
return dest;
|
169
|
+
}
|
170
|
+
else {
|
171
|
+
return rb_str_dup(str);
|
172
|
+
}
|
173
|
+
}
|
174
|
+
|
175
|
+
static unsigned char
|
176
|
+
url_unreserved_char(unsigned char c)
|
177
|
+
{
|
178
|
+
switch (c) {
|
179
|
+
case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9':
|
180
|
+
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h': case 'i': case 'j':
|
181
|
+
case 'k': case 'l': case 'm': case 'n': case 'o': case 'p': case 'q': case 'r': case 's': case 't':
|
182
|
+
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
|
183
|
+
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': case 'H': case 'I': case 'J':
|
184
|
+
case 'K': case 'L': case 'M': case 'N': case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T':
|
185
|
+
case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
|
186
|
+
case '-': case '.': case '_': case '~':
|
187
|
+
return 1;
|
188
|
+
default:
|
189
|
+
break;
|
190
|
+
}
|
191
|
+
return 0;
|
192
|
+
}
|
193
|
+
|
194
|
+
static VALUE
|
195
|
+
optimized_escape(VALUE str)
|
196
|
+
{
|
197
|
+
long i, len, beg = 0;
|
198
|
+
VALUE dest = 0;
|
199
|
+
const char *cstr;
|
200
|
+
char buf[4] = {'%'};
|
201
|
+
|
202
|
+
len = RSTRING_LEN(str);
|
203
|
+
cstr = RSTRING_PTR(str);
|
204
|
+
|
205
|
+
for (i = 0; i < len; ++i) {
|
206
|
+
const unsigned char c = (unsigned char)cstr[i];
|
207
|
+
if (!url_unreserved_char(c)) {
|
208
|
+
if (!dest) {
|
209
|
+
dest = rb_str_buf_new(len);
|
210
|
+
}
|
211
|
+
|
212
|
+
rb_str_cat(dest, cstr + beg, i - beg);
|
213
|
+
beg = i + 1;
|
214
|
+
|
215
|
+
if (c == ' ') {
|
216
|
+
rb_str_cat_cstr(dest, "+");
|
217
|
+
}
|
218
|
+
else {
|
219
|
+
buf[1] = upper_hexdigits[(c >> 4) & 0xf];
|
220
|
+
buf[2] = upper_hexdigits[c & 0xf];
|
221
|
+
rb_str_cat(dest, buf, 3);
|
222
|
+
}
|
223
|
+
}
|
224
|
+
}
|
225
|
+
|
226
|
+
if (dest) {
|
227
|
+
rb_str_cat(dest, cstr + beg, len - beg);
|
228
|
+
preserve_original_state(str, dest);
|
229
|
+
return dest;
|
230
|
+
}
|
231
|
+
else {
|
232
|
+
return rb_str_dup(str);
|
233
|
+
}
|
234
|
+
}
|
235
|
+
|
236
|
+
static VALUE
|
237
|
+
optimized_unescape(VALUE str, VALUE encoding)
|
238
|
+
{
|
239
|
+
long i, len, beg = 0;
|
240
|
+
VALUE dest = 0;
|
241
|
+
const char *cstr;
|
242
|
+
rb_encoding *enc = rb_to_encoding(encoding);
|
243
|
+
int cr, origenc, encidx = rb_enc_to_index(enc);
|
244
|
+
|
245
|
+
len = RSTRING_LEN(str);
|
246
|
+
cstr = RSTRING_PTR(str);
|
247
|
+
|
248
|
+
for (i = 0; i < len; ++i) {
|
249
|
+
char buf[1];
|
250
|
+
const char c = cstr[i];
|
251
|
+
int clen = 0;
|
252
|
+
if (c == '%') {
|
253
|
+
if (i + 3 > len) break;
|
254
|
+
if (!ISXDIGIT(cstr[i+1])) continue;
|
255
|
+
if (!ISXDIGIT(cstr[i+2])) continue;
|
256
|
+
buf[0] = ((char_to_number(cstr[i+1]) << 4)
|
257
|
+
| char_to_number(cstr[i+2]));
|
258
|
+
clen = 2;
|
259
|
+
}
|
260
|
+
else if (c == '+') {
|
261
|
+
buf[0] = ' ';
|
262
|
+
}
|
263
|
+
else {
|
264
|
+
continue;
|
265
|
+
}
|
266
|
+
|
267
|
+
if (!dest) {
|
268
|
+
dest = rb_str_buf_new(len);
|
269
|
+
}
|
270
|
+
|
271
|
+
rb_str_cat(dest, cstr + beg, i - beg);
|
272
|
+
i += clen;
|
273
|
+
beg = i + 1;
|
274
|
+
|
275
|
+
rb_str_cat(dest, buf, 1);
|
276
|
+
}
|
277
|
+
|
278
|
+
if (dest) {
|
279
|
+
rb_str_cat(dest, cstr + beg, len - beg);
|
280
|
+
preserve_original_state(str, dest);
|
281
|
+
cr = ENC_CODERANGE_UNKNOWN;
|
282
|
+
}
|
283
|
+
else {
|
284
|
+
dest = rb_str_dup(str);
|
285
|
+
cr = ENC_CODERANGE(str);
|
286
|
+
}
|
287
|
+
origenc = rb_enc_get_index(str);
|
288
|
+
if (origenc != encidx) {
|
289
|
+
rb_enc_associate_index(dest, encidx);
|
290
|
+
if (!ENC_CODERANGE_CLEAN_P(rb_enc_str_coderange(dest))) {
|
291
|
+
rb_enc_associate_index(dest, origenc);
|
292
|
+
if (cr != ENC_CODERANGE_UNKNOWN)
|
293
|
+
ENC_CODERANGE_SET(dest, cr);
|
294
|
+
}
|
295
|
+
}
|
296
|
+
return dest;
|
297
|
+
}
|
298
|
+
|
299
|
+
/*
|
300
|
+
* call-seq:
|
301
|
+
* CGI.escapeHTML(string) -> string
|
302
|
+
*
|
303
|
+
* Returns HTML-escaped string.
|
304
|
+
*
|
305
|
+
*/
|
306
|
+
static VALUE
|
307
|
+
cgiesc_escape_html(VALUE self, VALUE str)
|
308
|
+
{
|
309
|
+
StringValue(str);
|
310
|
+
|
311
|
+
if (rb_enc_str_asciicompat_p(str)) {
|
312
|
+
return optimized_escape_html(str);
|
313
|
+
}
|
314
|
+
else {
|
315
|
+
return rb_call_super(1, &str);
|
316
|
+
}
|
317
|
+
}
|
318
|
+
|
319
|
+
/*
|
320
|
+
* call-seq:
|
321
|
+
* CGI.unescapeHTML(string) -> string
|
322
|
+
*
|
323
|
+
* Returns HTML-unescaped string.
|
324
|
+
*
|
325
|
+
*/
|
326
|
+
static VALUE
|
327
|
+
cgiesc_unescape_html(VALUE self, VALUE str)
|
328
|
+
{
|
329
|
+
StringValue(str);
|
330
|
+
|
331
|
+
if (rb_enc_str_asciicompat_p(str)) {
|
332
|
+
return optimized_unescape_html(str);
|
333
|
+
}
|
334
|
+
else {
|
335
|
+
return rb_call_super(1, &str);
|
336
|
+
}
|
337
|
+
}
|
338
|
+
|
339
|
+
/*
|
340
|
+
* call-seq:
|
341
|
+
* CGI.escape(string) -> string
|
342
|
+
*
|
343
|
+
* Returns URL-escaped string.
|
344
|
+
*
|
345
|
+
*/
|
346
|
+
static VALUE
|
347
|
+
cgiesc_escape(VALUE self, VALUE str)
|
348
|
+
{
|
349
|
+
StringValue(str);
|
350
|
+
|
351
|
+
if (rb_enc_str_asciicompat_p(str)) {
|
352
|
+
return optimized_escape(str);
|
353
|
+
}
|
354
|
+
else {
|
355
|
+
return rb_call_super(1, &str);
|
356
|
+
}
|
357
|
+
}
|
358
|
+
|
359
|
+
static VALUE
|
360
|
+
accept_charset(int argc, VALUE *argv, VALUE self)
|
361
|
+
{
|
362
|
+
if (argc > 0)
|
363
|
+
return argv[0];
|
364
|
+
return rb_cvar_get(CLASS_OF(self), id_accept_charset);
|
365
|
+
}
|
366
|
+
|
367
|
+
/*
|
368
|
+
* call-seq:
|
369
|
+
* CGI.unescape(string, encoding=@@accept_charset) -> string
|
370
|
+
*
|
371
|
+
* Returns URL-unescaped string.
|
372
|
+
*
|
373
|
+
*/
|
374
|
+
static VALUE
|
375
|
+
cgiesc_unescape(int argc, VALUE *argv, VALUE self)
|
376
|
+
{
|
377
|
+
VALUE str = (rb_check_arity(argc, 1, 2), argv[0]);
|
378
|
+
|
379
|
+
StringValue(str);
|
380
|
+
|
381
|
+
if (rb_enc_str_asciicompat_p(str)) {
|
382
|
+
VALUE enc = accept_charset(argc-1, argv+1, self);
|
383
|
+
return optimized_unescape(str, enc);
|
384
|
+
}
|
385
|
+
else {
|
386
|
+
return rb_call_super(argc, argv);
|
387
|
+
}
|
388
|
+
}
|
389
|
+
|
390
|
+
void
|
391
|
+
Init_escape(void)
|
392
|
+
{
|
393
|
+
id_accept_charset = rb_intern_const("@@accept_charset");
|
394
|
+
InitVM(escape);
|
395
|
+
}
|
396
|
+
|
397
|
+
void
|
398
|
+
InitVM_escape(void)
|
399
|
+
{
|
400
|
+
rb_cCGI = rb_define_class("CGI", rb_cObject);
|
401
|
+
rb_mEscape = rb_define_module_under(rb_cCGI, "Escape");
|
402
|
+
rb_mUtil = rb_define_module_under(rb_cCGI, "Util");
|
403
|
+
rb_define_method(rb_mEscape, "escapeHTML", cgiesc_escape_html, 1);
|
404
|
+
rb_define_method(rb_mEscape, "unescapeHTML", cgiesc_unescape_html, 1);
|
405
|
+
rb_define_method(rb_mEscape, "escape", cgiesc_escape, 1);
|
406
|
+
rb_define_method(rb_mEscape, "unescape", cgiesc_unescape, -1);
|
407
|
+
rb_prepend_module(rb_mUtil, rb_mEscape);
|
408
|
+
rb_extend_object(rb_cCGI, rb_mEscape);
|
409
|
+
}
|