Пример #1
0
bool GameWorld::copyParameterTable(const Sqrat::Table &in, Sqrat::Table &out) {
  sq::Object::iterator it;
  while (in.Next(it)) {
    sq::Object value = in.GetSlot(it.getName());
    switch (value.GetType()) {
      case OT_NULL:
      {
        out.SetValue(it.getName(), nullptr);
      }
        break;
      case OT_BOOL:
      {
        out.SetValue(it.getName(), value.Cast<bool>());
      }
        break;
      case OT_INTEGER:
      {
        out.SetValue(it.getName(), value.Cast<int>());
      }
        break;
      case OT_FLOAT:
      {
        out.SetValue(it.getName(), value.Cast<float>());
      }
        break;
      case OT_STRING:
      {
        out.SetValue(it.getName(), value.Cast<std::string>());
      }
        break;
      default:
        std::cout<<"Attemt to pass unsafe parameter to game world";
    }
  }
  return true;
}
Пример #2
0
bool  SqMod::construct(CtxMod* pctx)
{
    _pctx = pctx;
    _header_sent=false;
    if(_penv) {
        _penv->acquire();

        Sqrat::Class<SqMod> module;
        module.Func(_SC("write"), &SqMod::swrite);
        Sqrat::RootTable().SetInstance(_SC("_this"), this);

        //
        // _context
        //
        Sqrat::Table context;
        //Sqrat::Class<CtxMod> context;
        context.SetValue(_SC("urldoc"), pctx->_url_doc)
        .SetValue(_SC("urldir"),  pctx->_url_path)
        .SetValue(_SC("home"),    pctx->_dir_home)
        .SetValue(_SC("index"),   pctx->_doc_index)
        .SetValue(_SC("ip"),      pctx->_cli_ip);
        Sqrat::RootTable().Bind(_SC("_context"), context);

        //
        // _REQ table with _ARGS, GET, COOKIE and so on
        //
        Sqrat::Table headers;
        const KeyValx* ps = _pctx->_hdrs;
        while((ps)->key) {
            if(ps->val && *ps->val) {
                headers.SetValue(_SC(ps->key), _SC(ps->val));
            }
            ++ps;
        }
        Sqrat::RootTable().Bind(_SC("_request"), headers);


        Sqrat::Table ckooks;
        const KeyValx* psc = _pctx->_cookies;
        while((psc)->key) {
            if(psc->val && *psc->val) {
                ckooks.SetValue(_SC(psc->key), _SC(psc->val));
            }
            ++psc;
        }
        Sqrat::RootTable().Bind(_SC("_cookies"), ckooks);

        Sqrat::Table get;
        const KeyValx* psv = _pctx->_get;
        while((psv)->key) {
            if(psv->val && *psv->val) {
                get.SetValue(_SC(psv->key), _SC(psv->val));
            }
            ++psv;
        }
        Sqrat::RootTable().Bind(_SC("_get"), get);
        //Sqrat::RootTable().SetInstance(_SC("_ctx"), pctx);

        Sqrat::Table post;

        if(pctx->_tempfile) {
            // parse the post
            /*
            Content-Type: application/x-www-form-urlencoded
            Content-Length: 53

            realname=ssssssss&email=ddddddddddd&comments=wwwwwwww
            */
            const char* astype = _getHdr("CONTENT-TYPE")->val;
            int64_t len = atoll(_getHdr("CONTENT-LENGTH")->val);
            if(!str_cmp(astype,"application/x-www-form-urlencoded") && len < 16384) {
                char* buff = new char[len + 1];
                if(buff) {
                    int bytes = fread(buff, 1, len, pctx->_tempfile);
                    char* pwalk = buff;
                    assert(bytes == len);
                    buff[bytes] = 0;
                    do {
                        const char* key = str_up2chr(&pwalk, '=');
                        const char* val = str_up2chr(&pwalk, '&');
                        if(!valid_ptr(key) ||  !valid_ptr(val))
                            break;
                        post.SetValue(_SC(key), _SC(val));
                    } while(1);
                    delete []buff;
                }
            }
        }
        Sqrat::RootTable().Bind(_SC("_post"), post);
        return true;
    }
    return false;
}