예제 #1
0
/**
* @date     2011/12/21
*
*  Handler of a request. The WebService request si built from the request, and
*  forwarded to the correct WsInterface.
*
******************************************************************************/
int32_t  WsHandler::RequestReceived(const HttpdRequest &Request,
                                    HttpdRequest *Answer)
{
   int32_t i_Ret = -1;
   ListIterator<AList<WsInterface*>, WsInterface*>  Iterator;
   WsInterface    *p_Interface = NULL;
   const String   *RequestInterface;

   if(HandleCors(Request, Answer) == 0)
      return(0);

   DynObject   Params;
   const uint8_t *Payload;
   uint32_t i_Length;
   i_Ret = Request.GetPayload(&Payload, &i_Length);
   if(i_Ret == 0)
   {
      JsonFlattener Flattener;
      const String PayloadString((const char*)(Payload), i_Length);

      i_Ret = Flattener.UnFlatten(&PayloadString, &Params);
      if(i_Ret == 0)
      {
         i_Ret = Params.FindString("InterfaceName", &RequestInterface);
      }
   }
   if(i_Ret != 0)
      return(-1);

   Iterator.SetTo(&InterfaceList);
   Iterator.First();
   while(Iterator.IsDone() == false)
   {
      if( (Iterator.GetItem(&p_Interface) == 0) && (p_Interface != NULL) )
      {
         if(p_Interface->GetName() == *RequestInterface)
         {
            String   AnswerString;
            i_Ret = p_Interface->Call(Params, &AnswerString);
            if(i_Ret == 0)
            {
               i_Ret = Answer->SetPayload((const uint8_t*)AnswerString.GetString(),
                                          AnswerString.GetSize());
            }
            break;
         }
      }
      Iterator.Next();
   }

   return(i_Ret);
}
예제 #2
0
TEST(HttpdRequestTestGroup, Basic)
{
   String   SetPath(PATH1 );
   HttpdRequest Obj1;

   /* Path */
   const String   &GetPath1 = Obj1.GetPath();
   CHECK(GetPath1 == "");
   CHECK(Obj1.SetPath(SetPath) == 0);

   const String   &GetPath2 = Obj1.GetPath();
   CHECK(GetPath2 == SetPath);

   /* Status */
   CHECK(Obj1.GetStatus() == 200);
   Obj1.SetStatus(404);
   CHECK(Obj1.GetStatus() == 404);

   /* Method */
   CHECK(Obj1.GetMethod() == HttpdRequest::NONE);
   Obj1.SetMethod(HttpdRequest::POST);
   CHECK(Obj1.GetMethod() == HttpdRequest::POST);

   /* payload */
   uint8_t SetPayload[326];
   const uint8_t *GetPayload;
   uint32_t i_Length;
   CHECK(Obj1.SetPayload(SetPayload, 326) == 0);
   CHECK(Obj1.GetPayload(&GetPayload, &i_Length) == 0);
   CHECK(i_Length == 326);
   CHECK(memcmp(GetPayload, SetPayload, i_Length) == 0);


   /* Header */
   String Header1Name(HEADER1), Header2Name(HEADER2);
   String Header1Val(HEADER1VAL), Header2Val(HEADER2VAL);
   String HeaderVal;

   CHECK(Obj1.GetHeader(Header1Name, &HeaderVal) == -1);
   CHECK(Obj1.SetHeader(Header1Name, Header1Val) == 0);
   CHECK(Obj1.GetHeader(Header1Name, &HeaderVal) == 0);
   CHECK(HeaderVal == Header1Val);

   CHECK(Obj1.SetHeader(Header2Name, Header2Val) == 0);
   CHECK(Obj1.GetHeader(Header2Name, &HeaderVal) == 0);
   CHECK(HeaderVal == Header2Val);

   CHECK(Obj1.SetHeader(Header1Name, Header2Val) == 0);
   CHECK(Obj1.GetHeader(Header1Name, &HeaderVal) == 0);
   CHECK(HeaderVal == Header2Val);

   /* Header list */
   const AList<HttpdRequest::Header*> &HeaderList = Obj1.GetHeaderList();
   ListIterator<AList<HttpdRequest::Header*>, HttpdRequest::Header*> Iterator;
   HttpdRequest::Header  *p_CurHeader;
   uint32_t i_Index = 0;

   Iterator.SetTo(&HeaderList);
   Iterator.First();
   while(Iterator.IsDone() == false)
   {
      CHECK(Iterator.GetItem(&p_CurHeader) == 0);
      CHECK(p_CurHeader != NULL);

      CHECK(i_Index <= 1);
      switch(i_Index)
      {
         case 0:
            CHECK(p_CurHeader->Name == Header1Name);
            CHECK(p_CurHeader->Value == Header2Val);
         break;
         case 1:
            CHECK(p_CurHeader->Name == Header2Name);
            CHECK(p_CurHeader->Value == Header2Val);
         break;
      }
      i_Index++;
      Iterator.Next();
   }

};