mirror of
https://github.com/mkxp-z/mkxp-z.git
synced 2025-09-03 12:43:04 +02:00
set HTTP response body encoding based on Content-Type
This commit is contained in:
parent
d7816c121b
commit
f3f401a794
2 changed files with 21 additions and 1 deletions
|
@ -170,6 +170,7 @@ return Data_Wrap_Struct(klass, 0, free, 0); \
|
||||||
|
|
||||||
#if RAPI_MINOR < 9
|
#if RAPI_MINOR < 9
|
||||||
#define rb_utf8_str_new_cstr rb_str_new2
|
#define rb_utf8_str_new_cstr rb_str_new2
|
||||||
|
#define rb_utf8_str_new rb_str_new
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define PRIsVALUE "s"
|
#define PRIsVALUE "s"
|
||||||
|
|
|
@ -38,6 +38,25 @@ mkxp_net::StringMap hash2StringMap(VALUE hash) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VALUE getResponseBody(mkxp_net::HTTPResponse &res) {
|
||||||
|
#if RAPI_FULL >= 190
|
||||||
|
auto it = res.headers().find("Content-Type");
|
||||||
|
if (it == res.headers().end())
|
||||||
|
return rb_str_new(res.body().c_str(), res.body().length());
|
||||||
|
|
||||||
|
std::string &ctype = it->second;
|
||||||
|
|
||||||
|
if (!ctype.compare("text/plain") || !ctype.compare("application/json") ||
|
||||||
|
!ctype.compare("application/xml") || !ctype.compare("text/html") ||
|
||||||
|
!ctype.compare("text/css") || !ctype.compare("text/javascript") ||
|
||||||
|
!ctype.compare("application/x-sh") || !ctype.compare("image/svg+xml") ||
|
||||||
|
!ctype.compare("application/x-httpd-php"))
|
||||||
|
return rb_utf8_str_new(res.body().c_str(), res.body().length());
|
||||||
|
|
||||||
|
#endif
|
||||||
|
return rb_str_new(res.body().c_str(), res.body().length());
|
||||||
|
}
|
||||||
|
|
||||||
RB_METHOD(httpGet) {
|
RB_METHOD(httpGet) {
|
||||||
RB_UNUSED_PARAM;
|
RB_UNUSED_PARAM;
|
||||||
|
|
||||||
|
@ -56,7 +75,7 @@ RB_METHOD(httpGet) {
|
||||||
auto res = req.get();
|
auto res = req.get();
|
||||||
ret = rb_hash_new();
|
ret = rb_hash_new();
|
||||||
rb_hash_aset(ret, ID2SYM(rb_intern("status")), INT2NUM(res.status()));
|
rb_hash_aset(ret, ID2SYM(rb_intern("status")), INT2NUM(res.status()));
|
||||||
rb_hash_aset(ret, ID2SYM(rb_intern("body")), rb_utf8_str_new(res.body().c_str(), res.body().length()));
|
rb_hash_aset(ret, ID2SYM(rb_intern("body")), getResponseBody(res));
|
||||||
rb_hash_aset(ret, ID2SYM(rb_intern("headers")), stringMap2hash(res.headers()));
|
rb_hash_aset(ret, ID2SYM(rb_intern("headers")), stringMap2hash(res.headers()));
|
||||||
} catch (Exception &e) {
|
} catch (Exception &e) {
|
||||||
raiseRbExc(e);
|
raiseRbExc(e);
|
||||||
|
|
Loading…
Add table
Reference in a new issue