Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.encoding;
- import std.stdio;
- import std.string;
- import std.conv;
- import std.algorithm;
- import std.stream;
- /*
- Specs:
- · El body puede ser un ubyte[] (de un inputstream, un cfile, etc) o un string
- · Si es un ubyte[] el get_body() devuelve el ubyte[] tal cual (leyendo del inputstream si es necesario)
- · Si es un string, el get_body() convierte el _charset indicado y se hace un cast a ubyte[] al devolver
- */
- static immutable default_charset = "Latin-1";
- class ResponseBody
- {
- this(ubyte[] data)
- {
- _ubyte_content = data; // .dup? .idup?
- _store_type = ContentStore.BYTEARRAY;
- }
- this(string data)
- {
- _string_content = data;
- _store_type = ContentStore.STRING;
- }
- this(InputStream istream)
- {
- _istream_content = istream;
- _store_type = ContentStore.ISTREAM;
- }
- this()
- {
- _store_type = ContentStore.EMPTY;
- }
- @property content(ubyte[] data)
- {
- _ubyte_content = data;
- _store_type = ContentStore.BYTEARRAY;
- // Clear the other _*_content members?
- }
- @property content(string data)
- {
- _string_content = data;
- _store_type = ContentStore.STRING;
- }
- @property content(InputStream istream)
- {
- _istream_content = istream;
- _store_type = ContentStore.ISTREAM;
- }
- ubyte[] get_content_as_bytes(string charset = default_charset)
- {
- ubyte[] output;
- // No conversion is done if read from a file or inputstream, the user should take care of that if needed
- if (_store_type == ContentStore.EMPTY)
- return output; // Throw exception instead?
- else if (_store_type == ContentStore.BYTEARRAY)
- output = _ubyte_content;
- else if (_store_type == ContentStore.STRING) {
- string charset_simp = charset.tolower().removechars("-");
- // is a string, encode into the specified encoding
- if (charset_simp == "utf8")
- output = cast(ubyte[])_string_content;
- if (charset_simp == "ascii") {
- AsciiString s;
- transcode(_string_content, s);
- output = cast(ubyte[]) s;
- }
- else if (charset_simp == "latin1") {
- Latin1String s;
- transcode(_string_content, s);
- output = cast(ubyte[]) s;
- }
- else if (charset_simp == "utf16") {
- wstring s;
- transcode(_string_content, s);
- output = cast(ubyte[]) s;
- }
- else if (charset_simp == "utf32") {
- dstring s;
- transcode(_string_content, s);
- output = cast(ubyte[]) s;
- }
- else
- throw new Exception("Encoding " ~ charset ~ " is not supported");
- }
- // XXX TEST
- else if (_store_type == ContentStore.ISTREAM) {
- ubyte[1024] partial;
- output.length = 1024*128;
- uint readed, totalread;
- while ((readed = _istream_content.read(partial)) > 0) {
- if (output.length < totalread+readed)
- output.length *= 2;
- //output ~= partial[0..readed];
- output[totalread..totalread+readed] = partial[0..readed];
- totalread += readed;
- }
- output.length = totalread;
- }
- return output;
- }
- // XXX TEST
- InputStream get_content_as_istream() {
- if (_store_type == ContentStore.EMPTY) {
- ubyte[] empty;
- return new MemoryStream(empty); // Throw exception instead?
- }
- else if (_store_type == ContentStore.BYTEARRAY)
- return new MemoryStream(get_content_as_bytes());
- else if (_store_type == ContentStore.STRING)
- return new MemoryStream( to!(char[])(_string_content) );
- else if (_store_type == ContentStore.ISTREAM)
- return _istream_content;
- assert(0);
- }
- private:
- InputStream _istream_content;
- ubyte[] _ubyte_content;
- string _string_content;
- enum ContentStore { EMPTY, STRING, BYTEARRAY, ISTREAM };
- ContentStore _store_type = ContentStore.STRING;
- }
- ubyte[] byte_array_encoded_in(string input, string encoding) {
- auto output = EncodingScheme.create(encoding);
- writeln(typeid(output));
- writeln(output);
- //transcode(input, output);
- //return cast(ubyte[]) output;
- ubyte[] a;
- return a;
- }
- // convertir en unittest
- void main() {
- ubyte[] bytearray = cast(ubyte[]) (cast(Latin1String)"Esto es un fichero de texto con codificación Latin1");
- string ascii_chars_in_utf = "En un lugar de la mancha";
- string latin1_chars_in_utf = "ñaña";
- // ubyte[] input, utf-8 output
- auto b = new ResponseBody(bytearray);
- ubyte[] bodybytes = b.get_content_as_bytes("utf-8");
- assert(cast(string)bodybytes == bytearray);
- // utf-8 input, ascii output
- auto a = new ResponseBody(ascii_chars_in_utf);
- bodybytes = a.get_content_as_bytes("ascii");
- AsciiString ascii_chars;
- transcode(ascii_chars_in_utf, ascii_chars);
- assert(cast(AsciiString)bodybytes == ascii_chars);
- // utf-8 input, latin1 output
- auto l = new ResponseBody(latin1_chars_in_utf);
- bodybytes = l.get_content_as_bytes("latin-1");
- Latin1String latin1_chars;
- transcode(latin1_chars_in_utf, latin1_chars);
- assert(cast(Latin1String)bodybytes == latin1_chars);
- // utf-8 input, utf-16 output
- auto w = new ResponseBody("ñaña w16");
- bodybytes = w.get_content_as_bytes("utf-16");
- assert(cast(wstring)bodybytes == "ñaña w16"w);
- // utf-8 input, utf-32 output
- auto d = new ResponseBody("ñaña w32");
- bodybytes = d.get_content_as_bytes("utf-32");
- assert(cast(dstring)bodybytes == "ñaña w32"d);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement