Exemple #1
0
int tun_read_ev(int idx, dbuf_t *d, void *p) {
  lua_State *L;
  http_parser_t *parser = &http_parser;
  http_parser_init(parser);
  L = lua_open();
  lua_init_state(L);
  if(http_parser_data(parser, d->buf, d->dsize)) {
    char *reply = "File not found!\n";
    char *content_type = "text/plain";
    char headers[512];
    char trailer[1024];
    char *xfile;
    size_t xfile_sz;

    debug(DBG_GLOBAL, 1, "Parser done. Method: '%s', URI: '%s'", parser->req_method, parser->req_uri);
    if(strstr(parser->req_uri, ".css")) {
      content_type = "text/css";
    } else if(strstr(parser->req_uri, ".html")) {
      content_type = "text/html";
    } else if(strstr(parser->req_uri, ".js")) {
      content_type = "application/javascript";
    } else if(strstr(parser->req_uri, ".png")) {
      content_type = "image/png";
    }
    memcpy(trailer, parser->end, parser->content_length);
    trailer[parser->content_length] = 0;
    debug(DBG_GLOBAL, 0, "Parser trailer: '%s'", trailer);

    xfile = mz_zip_extract_archive_file_to_heap("data.zip", parser->req_uri+1, &xfile_sz, 0);
    if(xfile) {
      debug(0, 0, "File '%s' => size is %d", parser->req_uri+1, xfile_sz);
      xfile[xfile_sz] = 0;
      if(strstr(parser->req_uri, ".lua")) {
        if(luaL_loadstring(L, xfile)) {
	  debug(0, 0, "Lua file load error: %s", lua_tostring(L,-1));
        } else {
          lua_pcall(L, 0, LUA_MULTRET, 0);
          lua_getglobal(L, "request");
	  lua_pushinteger(L, idx);
	  int err = lua_pcall(L, 1, 0, 0);
          if (err != 0) {
            debug(0,0,"%d: LUA error %s\n",getpid(), lua_tostring(L,-1));
          }
        }
      } else {
        snprintf(headers, sizeof(headers)-1, "HTTP/1.0 200 OK\r\nConnection: keep-alive\r\nContent-length: %d\r\nContent-type: %s\r\n\r\n", (int)xfile_sz, content_type);
        dunlock(sock_write_data(idx, dstrcpy(headers), NULL));
        dunlock(sock_write_data(idx, dalloc_ptr(xfile, xfile_sz), NULL));
	xfile = NULL;
      }
    } else {
      snprintf(headers, sizeof(headers)-1, "HTTP/1.0 404 Not Found\r\nConnection: keep-alive\r\nContent-length: %d\r\nContent-type: %s\r\n\r\n", (int)strlen(reply), content_type);
      dunlock(sock_send_data(idx, dstrcpy(headers), NULL));
      dunlock(sock_send_data(idx, dstrcpy(reply), NULL));
    }
    if(xfile) {
      free(xfile);
    }
    lua_close(L);
  } else {
    lua_close(L);
    return 0;
  }
  
  // printf("Got packet, sending back!\n");
  return -1;
}
Exemple #2
0
//---------------------------------------------------------------------------
STATIC char* ZipFile::LoadFileFromZipArchiveOnDiskToHeap( const char* zipFileLocation, const char* fileLocationInsideZip, size_t& out_fileSize )
{
	char* buffer = nullptr;
	buffer = reinterpret_cast< char* > ( mz_zip_extract_archive_file_to_heap( zipFileLocation, fileLocationInsideZip, &out_fileSize, 0 ) );
	return buffer;
}