| 49 | } |
| 50 | |
| 51 | dmScript::Result HttpResponseDecoder(lua_State* L, const dmDDF::Descriptor* desc, const char* data) |
| 52 | { |
| 53 | assert(desc == dmHttpDDF::HttpResponse::m_DDFDescriptor); |
| 54 | dmHttpDDF::HttpResponse* resp = (dmHttpDDF::HttpResponse*) data; |
| 55 | |
| 56 | char* headers = (char*) resp->m_Headers; |
| 57 | char* response = (char*) resp->m_Response; |
| 58 | |
| 59 | lua_newtable(L); |
| 60 | |
| 61 | lua_pushinteger(L, resp->m_Status); |
| 62 | lua_setfield(L, -2, "status"); |
| 63 | |
| 64 | if (resp->m_Path) |
| 65 | { |
| 66 | if (resp->m_Status == 200) { |
| 67 | if (!WriteResponseToFile(resp->m_Path, response, resp->m_ResponseLength)) |
| 68 | { |
| 69 | lua_pushliteral(L, "Failed to write to temp file"); |
| 70 | lua_setfield(L, -2, "error"); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | lua_pushstring(L, resp->m_Path); |
| 75 | lua_setfield(L, -2, "path"); |
| 76 | } else { |
| 77 | lua_pushlstring(L, response, resp->m_ResponseLength); |
| 78 | lua_setfield(L, -2, "response"); |
| 79 | } |
| 80 | |
| 81 | if (resp->m_Url) |
| 82 | { |
| 83 | lua_pushstring(L, resp->m_Url); |
| 84 | lua_setfield(L, -2, "url"); |
| 85 | } |
| 86 | |
| 87 | lua_pushinteger(L, resp->m_RangeStart); |
| 88 | lua_setfield(L, -2, "range_start"); |
| 89 | lua_pushinteger(L, resp->m_RangeEnd); |
| 90 | lua_setfield(L, -2, "range_end"); |
| 91 | lua_pushinteger(L, resp->m_DocumentSize); |
| 92 | lua_setfield(L, -2, "document_size"); |
| 93 | |
| 94 | lua_pushliteral(L, "headers"); |
| 95 | lua_newtable(L); |
| 96 | if (resp->m_HeadersLength > 0) { |
| 97 | headers[resp->m_HeadersLength-1] = '\0'; |
| 98 | |
| 99 | char* s, *last; |
| 100 | s = dmStrTok(headers, "\n", &last); |
| 101 | while (s) { |
| 102 | char* colon = strchr(s, ':'); |
| 103 | *colon = '\0'; |
| 104 | |
| 105 | // Convert attribute to lower-case |
| 106 | // for simplified handling (http header attributes are case-insensitive) |
| 107 | char* tmp = s; |
| 108 | while (*tmp) { |
nothing calls this directly
no test coverage detected