From 7b45c911ef05ca69b111f2c071b6661354768b90 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 9 Mar 2009 17:26:42 +0100 Subject: [PATCH] with great performance hit, a patch to handle binary (need to get the v8 people to produce something that doesn't require copying buffers 20 times.) --- node.cc | 23 +++++++++++++++++------ node_http.cc | 18 ++++++++++++++++-- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/node.cc b/node.cc index b76a462..583f8b2 100644 --- a/node.cc +++ b/node.cc @@ -20,23 +20,35 @@ static int exit_code = 0; static Handle ReadFile (const string& name) { + FILE* file = fopen(name.c_str(), "rb"); if (file == NULL) return Handle(); - + fseek(file, 0, SEEK_END); int size = ftell(file); rewind(file); - char* chars = new char[size + 1]; + char chars[size+1]; chars[size] = '\0'; for (int i = 0; i < size;) { int read = fread(&chars[i], 1, size - i, file); + if(read <= 0) { + perror("read()"); + } i += read; } + + uint16_t expanded_base[size+1]; + expanded_base[size] = '\0'; + for(int i = 0; i < size; i++) + expanded_base[i] = chars[i]; + fclose(file); - Handle result = String::New(chars, size); - delete[] chars; - return result; + + HandleScope scope; + Local result = String::New(expanded_base, size); + + return scope.Close(result); } static Handle @@ -61,7 +73,6 @@ BlockingFileRead (const Arguments& args) HandleScope scope; String::Utf8Value filename(args[0]); - Handle output = ReadFile (*filename); return scope.Close(output); } diff --git a/node_http.cc b/node_http.cc index df5a858..fcc032b 100644 --- a/node_http.cc +++ b/node_http.cc @@ -160,7 +160,14 @@ HttpRequest::Respond (Handle data) } else { Handle s = data->ToString(); oi_buf *buf = oi_buf_new2(s->Length()); - s->WriteAscii(buf->base, 0, s->Length()); + + uint16_t expanded[s->Length()]; + s->Write(expanded, 0, s->Length()); + + for(int i = 0; i < s->Length(); i++) { + buf->base[i] = expanded[i]; + } + output.push_back(buf); } connection.Write(); @@ -349,7 +356,14 @@ HttpRequest::MakeBodyCallback (const char *base, size_t length) if(length) { // TODO ByteArray? - Handle chunk = String::New(base, length); + // + + uint16_t expanded_base[length]; + for(int i = 0; i < length; i++) { + expanded_base[i] = base[i]; + } + + Handle chunk = String::New(expanded_base, length); argv[0] = chunk; } else { argv[0] = Null(); -- 2.7.4