Exemplo n.º 1
0
spx_private void ParserRequest(EV_P_ ev_async *watcher, int revents){/*{{{*/
    ev_async_stop(loop, watcher);
    err_t err = 0;
    struct server_context * ctx = (struct server_context *) watcher->data;
    if(NULL == ctx){
        SpxLog1(g_log, SpxLogError, "ParserRequest ctx is NULL");
        return; 
    }

   printf("\n----------------CLIENT:%d xxxxxxxxxxxxxxxxxx CTX:%d-----------------------\n", ctx->fd, GetCTXCount());
   if(NULL != ctx->request){
        //msg_print(ctx->request, ctx->req_size);
       
        long handle_size = ctx->ServerHandler(ctx->req_size, ctx->request, &ctx->response);
        if (-1 == handle_size){
            RequestException(ctx, bad_request);
        } else{
            ctx->resp_size = handle_size;
            ctx->life_cycle = SEND_RESPONSE;
            RegisterAayncWatcher(&ctx->async_watcher,SendResponse, ctx);
            ev_async_start(loop, &ctx->async_watcher);
            ev_async_send(loop, &ctx->async_watcher);
        }
   } else{
        RequestException(ctx, bad_request);
   }
}/*}}}*/
Exemplo n.º 2
0
spx_private void Sender_ReWriteResponse(int revents, void *arg){/*{{{*/
    struct server_context *ctx = (struct server_context *) arg;  
    if(NULL == ctx){
        SpxLog1(g_log, SpxLogError, "Sender ctx is NULL");
        return;
    }

    if(EV_ERROR & revents){
        SpxLog1(g_log, SpxLogError, "EV_ERROR");
        return;
    }

    if(EV_TIMEOUT & revents){
      if((ctx->resp_retry++) >= RETRY_TIMES){
        RequestException(ctx, bad_request);
        SpxLog1(g_log, SpxLogError, "EV_TIMEOUT");
        return;
      }else{
        ev_once(main_socket_loop, ctx->fd, EV_WRITE, ctx->timeout, Sender_ReWriteResponse, ctx);
        return;
      }
   }
  
   if(EV_WRITE & revents){
     err_t err = Sender_WriteResponse(ctx->fd, ctx->response, &ctx->resp_len, &ctx->split_size);        
     if((0 == err)&&(0 == ctx->split_size)){
          if(ctx->resp_size == ctx->resp_len){
                RequestFinish(ctx);             
          }else{
            int remain_size = ctx->resp_size - ctx->resp_len;
            if(remain_size >= SPLIT_SIZE){
                ctx->split_size = SPLIT_SIZE;
            }else{
                ctx->split_size = remain_size;
            }

            RegisterAayncWatcher(&ctx->async_watcher, Sender, ctx);
            ev_async_start(main_socket_loop, &ctx->async_watcher);
            ev_async_send(main_socket_loop, &ctx->async_watcher);
            return;
          }
     }else{
          if((EAGAIN == err || EWOULDBLOCK == err || EINTR == err)||(ctx->resp_size > 0)) {
              ev_once(main_socket_loop, ctx->fd, EV_READ, ctx->timeout, Sender_ReWriteResponse, ctx);
              return;
          }else{
             SpxLog2(g_log, SpxLogError, err,"Sender Failed");
             RequestException(ctx, bad_request);
          }
     }
   }
}/*}}}*/
Exemplo n.º 3
0
spx_private void SendResponse(EV_P_ ev_async *watcher, int revents){/*{{{*/
        ev_async_stop(loop, watcher);
        err_t err = 0;
        struct server_context * ctx = (struct server_context *) watcher->data;
        if(NULL == ctx){
            SpxLog1(g_log, SpxLogError, "SendResponse ctx is NULL");
            return; 
        }

        if(NULL != ctx->response){
            //headers(ctx->fd, ctx->resp_size);

            int remain_size = ctx->resp_size - ctx->resp_len;
            if(remain_size >= SPLIT_SIZE){
                ctx->split_size = SPLIT_SIZE;
            }else{
                ctx->split_size = remain_size;
            }

            RegisterAayncWatcher(&ctx->async_watcher, Sender, ctx);
            ev_async_start(loop, &ctx->async_watcher);
            ev_async_send(loop, &ctx->async_watcher);
        }else{
            RequestException(ctx, bad_request);
        }
}/*}}}*/
Exemplo n.º 4
0
spx_private void Sender(EV_P_ ev_async *watcher, int revents){/*{{{*/
    ev_async_stop(loop, watcher);
    struct server_context * ctx = (struct server_context *) watcher->data;
    if(NULL == ctx){
        SpxLog1(g_log, SpxLogError, "Sender ctx is NULL");
        return; 
    }

    err_t err = Sender_WriteResponse(ctx->fd, ctx->response, &ctx->resp_len, &ctx->split_size);        
    if((0 == err)&&(0 == ctx->split_size)){
         if(ctx->resp_size == ctx->resp_len){
            RequestFinish(ctx);             
         }else{
            int remain_size = ctx->resp_size - ctx->resp_len;
            if(remain_size >= SPLIT_SIZE){
                ctx->split_size = SPLIT_SIZE;
            }else{
                ctx->split_size = remain_size;
            }

            RegisterAayncWatcher(&ctx->async_watcher, Sender, ctx);
            ev_async_start(loop, &ctx->async_watcher);
            ev_async_send(loop, &ctx->async_watcher);
            return;
         }
    }else{
         if((EAGAIN == err || EWOULDBLOCK == err || EINTR == err)||(ctx->resp_size > 0)) {
             ev_once(main_socket_loop, ctx->fd, EV_READ, ctx->timeout, Sender_ReWriteResponse, ctx);
             return;
         }else{
           SpxLog2(g_log, SpxLogError, err,"Sender Failed");
           RequestException(ctx, bad_request);
         }
    }
}/*}}}*/
Exemplo n.º 5
0
spx_private void ReciveRequest(EV_P_ ev_async *watcher, int revents){/*{{{*/
    ev_async_stop(loop, watcher);
    struct server_context * ctx = (struct server_context *) watcher->data;
    if(NULL == ctx){
        SpxLog1(g_log, SpxLogError, "ReciveRequest ctx is NULL");
        return; 
    }
    err_t err = ReciveRequest_GetRequest_ReadRequest(ctx->fd, ctx->request, &ctx->req_len);        
    if(0 == err){
        RegisterAayncWatcher(&ctx->async_watcher, ParserRequest, ctx);
        ev_async_start(loop, &ctx->async_watcher);
        ev_async_send(loop, &ctx->async_watcher);
        printf("buf:%s\n",ctx->request);
        return;
    }else{
        if(EAGAIN == err || EWOULDBLOCK == err || EINTR == err) {
                ev_once(loop, ctx->fd, EV_READ, ctx->timeout, ReciveRequest_GetRequest, ctx);
                return;
        }else{
            SpxLog2(g_log, SpxLogError, err,"ReciveRequest_GetRequest_ReadRequest Failed");
            if( -1 == err)
                CloseCTX(ctx);
            else
                RequestException(ctx, bad_request);
            return;
        }
    }
}/*}}}*/
Exemplo n.º 6
0
spx_private void ReciveRequest_GetRequest(int revents, void *arg){/*{{{*/
    struct server_context *ctx = (struct server_context *) arg;
    if(NULL == ctx){
        SpxLog1(g_log, SpxLogError, "ReciveRequest_GetRequest ctx is NULL");
        return;
    }

    if(EV_ERROR & revents){
        SpxLog1(g_log, SpxLogError, "EV_ERROR");
        return;
    }
    
    if(EV_TIMEOUT & revents){
      if((ctx->req_retry++) >= RETRY_TIMES){
        RequestException(ctx, bad_request);
        SpxLog1(g_log, SpxLogError, "EV_TIMEOUT");
        return;
      }else{
        ev_once(main_socket_loop, ctx->fd, EV_READ, ctx->timeout, ReciveRequest_GetRequest, ctx);
        return;
      }
   }
  
   if(EV_READ & revents){
     err_t err = ReciveRequest_GetRequest_ReadRequest(ctx->fd, ctx->request, &ctx->req_len);        
     if(0 == err){
         RegisterAayncWatcher(&ctx->async_watcher, ParserRequest, ctx);
         ev_async_start(main_socket_loop, &ctx->async_watcher);
         ev_async_send(main_socket_loop, &ctx->async_watcher);
     }else{
          if((EAGAIN == err || EWOULDBLOCK == err || EINTR == err)) {
              ev_once(main_socket_loop, ctx->fd, EV_READ, ctx->timeout, ReciveRequest_GetRequest, ctx);
              return;
          }else{
              SpxLog2(g_log, SpxLogError, err,"ReciveRequest_GetRequest_ReadRequest Failed");
              if( -1 == err)
                  CloseCTX(ctx);
              else
                  RequestException(ctx, bad_request);
              return;
          }
     }
   }

}/*}}}*/
Exemplo n.º 7
0
		void MapRequest::setOutput( const std::string& value )
		{
			_output = value;

			if (!Factory<Renderer>::contains(_output))
			{
				throw RequestException ("Invalid map output type " + _output);
			}
		}
		void HikingTrailDisplayFunction::_setFromParametersMap(const ParametersMap& map)
		{
			_FunctionWithSite::_setFromParametersMap(map);
			try
			{
				_hikingTrail = Env::GetOfficialEnv().get<HikingTrail>(map.get<RegistryKeyType>(Request::PARAMETER_OBJECT_ID));
			}
			catch (ObjectNotFoundException<HikingTrail>)
			{
				throw RequestException("No such hiking trail");
			}
			_key = map.get<string>(PARAMETER_KEY);
		}
Exemplo n.º 9
0
	string FavoriteDirectoryApi::getPath(const ApiRequest& aRequest) {
		auto tth = aRequest.getTTHParam();
		auto dirs = FavoriteManager::getInstance()->getFavoriteDirs();
		auto p = boost::find_if(dirs | map_keys, [&](const string& aPath) {
			return AirUtil::getPathId(aPath) == tth;
		});

		if (p.base() == dirs.end()) {
			throw RequestException(websocketpp::http::status_code::not_found, "Directory not found");
		}

		return *p;
	}
Exemplo n.º 10
0
		void PDFImageService::_setFromParametersMap(const ParametersMap& map)
		{
			// The image
			_key = map.get<string>(PARAMETER_IMAGE);
			ParametersMap::Trim(_key);
			if(_key[0] >= '0' && _key[0] <= '9')
			{	// Font by ID
				try
				{
					RegistryKeyType pageId(lexical_cast<RegistryKeyType>(_key));
					_image = Env::GetOfficialEnv().get<Webpage>(pageId).get();
				}
				catch(bad_lexical_cast&)
				{
					throw RequestException("Bad cast in image id");
				}
				catch(ObjectNotFoundException<Webpage>&)
				{
					throw RequestException("No such image");
				}
			}
			else
			{	// Page by smart URL
				_image = getSite()->getPageBySmartURL(_key);
				if(!_image)
				{
					throw RequestException("No such image");
				}
			}

			// x/y
			_x = map.get<float>(PARAMETER_X);
			_y = map.get<float>(PARAMETER_Y);

			// width/height
			_width = map.getDefault<float>(PARAMETER_WIDTH, 0);
			_height = map.getDefault<float>(PARAMETER_HEIGHT, 0);
		}
Exemplo n.º 11
0
		void ServiceInformationsFunction::_setFromParametersMap(const ParametersMap& map)
		{
			// Service
			try
			{
				RegistryKeyType id(map.get<RegistryKeyType>(Request::PARAMETER_OBJECT_ID));
				_service = Env::GetOfficialEnv().get<ScheduledService>(id);
			}
			catch (ObjectNotFoundException<ScheduledService>& e)
			{
				throw RequestException("No such service : "+ e.getMessage());
			}
			catch(ParametersMap::MissingParameterException&)
			{
				throw RequestException("Service to display not specified");
			}

			// Stop page
			try
			{
				optional<RegistryKeyType> id(map.getOptional<RegistryKeyType>(PARAMETER_STOP_PAGE_ID));
				if(id)
				{
					_stopPage = Env::GetOfficialEnv().get<Webpage>(*id);
				}
			}
			catch (ObjectNotFoundException<Webpage>& e)
			{
				throw RequestException("No such stop page : "+ e.getMessage());
			}

			// Date
			if(!map.getDefault<string>(PARAMETER_DATE).empty())
			{
				_date = time_from_string(map.get<string>(PARAMETER_DATE));
			}
		}
Exemplo n.º 12
0
		void ObjectViewService::_setFromParametersMap(const ParametersMap& map)
		{
			// Object
			try
			{
				boost::shared_ptr<const Registrable> object(
					DBModule::GetObject(
						map.get<RegistryKeyType>(Request::PARAMETER_OBJECT_ID),
						Env::GetOfficialEnv()
				)	);
				if(!dynamic_cast<const ObjectBase*>(object.get()))
				{
					throw RequestException("Bad object type");
				}
				_object = dynamic_pointer_cast<const ObjectBase, const Registrable>(object);
			}
			catch(ObjectNotFoundException<ObjectBase>& e)
			{
				throw RequestException("No such object : "+ e.getMessage());
			}

			// Additional parameters
			_additionalParameters = map.getDefault<bool>(PARAMETER_ADDITIONAL_PARAMETERS, false);
		}
Exemplo n.º 13
0
void RequestWithInterface::_setFromParametersMap( const ParametersMap& map )
{
    try
    {
        // Interface
        RegistryKeyType id(map.getDefault<RegistryKeyType>(PARAMETER_INTERFACE, 0));
        if (id > 0)
        {
            _interface = Env::GetOfficialEnv().get<Interface>(id);
        }
    }
    catch (ObjectNotFoundException<Interface>& e)
    {
        throw RequestException("Specified interface not found : "+ e.getMessage());
    }
}
Exemplo n.º 14
0
		void ImportFunction::_setFromParametersMap(const ParametersMap& map)
		{
			// Import
			RegistryKeyType importId(map.get<RegistryKeyType>(PARAMETER_IMPORT_ID));
			try
			{
				boost::shared_ptr<const Import> import(ImportTableSync::Get(importId, *_env));
				if(!import->get<DataSource>())
				{
					throw RequestException("The id system of the specified import is not defined.");
				}

				// Log path
				bool outputLogs(map.getDefault<bool>(PARAMETER_OUTPUT_LOGS, false));

				// Min log force
				ImportLogLevel minLogLevel(import->get<MinLogLevel>());
				if(map.isDefined(PARAMETER_MIN_LOG_LEVEL))
				{
					minLogLevel = static_cast<ImportLogLevel>(map.get<int>(PARAMETER_MIN_LOG_LEVEL));
				}

				// Log path force
				string logPath(import->get<LogPath>());
				if(map.isDefined(PARAMETER_LOG_PATH))
				{
					logPath = map.get<string>(PARAMETER_LOG_PATH);
				}
				
				// Logger generation
				if(outputLogs)
				{
					// Importer generation
					_importer = import->getImporter(
						*_env,
						minLogLevel,
						logPath,
						_output,
						_result
					);
				}
				else
				{
					// Importer generation
					_importer = import->getImporter(
						*_env,
						minLogLevel,
						logPath,
						optional<ostream&>(),
						_result
					);
				}

				_importer->setFromParametersMap(map, true);

				_doImport = map.isTrue(PARAMETER_DO_IMPORT);
				_importDone = _importer->beforeParsing();
				_importDone &= _importer->parseFiles();
				_importDone &=_importer->afterParsing();
			}
			catch(ObjectNotFoundException<DataSource> e)
			{
				throw RequestException("Datasource not found");
			}
			catch(Exception e)
			{
				throw RequestException("Load failed : " + e.getMessage());
			}
		}
Exemplo n.º 15
0
spx_private void ReciveRequest_GetRequest(int revents, void *arg){/*{{{*/
    struct server_context *ctx = (struct server_context *) arg;
    if(NULL == ctx){
        SpxLog1(g_log, SpxLogError, "ReciveRequest_GetRequest ctx is NULL");
        return;
    }
    if(READ_HEADER == ctx->life_cycle){
         if(EV_ERROR & revents){
             SpxLog1(g_log, SpxLogError, "READ_HEADER EV_ERROR");
             return;
         }
         
         if(EV_TIMEOUT & revents){
           if((ctx->header_retry++) >= RETRY_TIMES){
             RequestException(ctx, bad_request);
             SpxLog1(g_log, SpxLogError, "READ_HEADER EV_TIMEOUT");
             return;
           } else{
             ev_once(main_socket_loop, ctx->fd, EV_READ, ctx->timeout, ReciveRequest_GetRequest, ctx);
             return;
           }
        }
  
        if(EV_READ & revents){
          char buf[LOG_HEADER_SIZE] = {0};
          size_t len = 0;
          err_t err = ReciveRequest_GetRequest_ReadRequest(ctx->fd, buf, &len, LOG_HEADER_SIZE);
          if(0 == err){
                log_header_unpack(buf, ctx->header);
                char * request = (char *) calloc(1, sizeof(char)*ctx->header->req_size); 
                if(NULL == request){
                      SpxLog2(g_log, SpxLogError, err,"calloc log_header failed");
                      return;
                }

                ctx->req_size = ctx->header->req_size;
                ctx->request = request;
                ctx->life_cycle = READ_REQUEST;
          } else{
               if((EAGAIN == err || EWOULDBLOCK == err || EINTR == err)) {
                   ev_once(main_socket_loop, ctx->fd, EV_READ, ctx->timeout, ReciveRequest_GetRequest, ctx);
                   return;
               } else{
                   SpxLog2(g_log, SpxLogError, err,"ReciveRequest_GetRequest_ReadRequest Failed");
                   CloseCTX(ctx);
                   return;
               }
          }
        }
    }

    if(READ_REQUEST == ctx->life_cycle){
         if(EV_ERROR & revents){
             SpxLog1(g_log, SpxLogError, "READ_REQUEST EV_ERROR");
             return;
         }
         
         if(EV_TIMEOUT & revents){
           if((ctx->req_retry++) >= RETRY_TIMES){
             RequestException(ctx, bad_request);
             SpxLog1(g_log, SpxLogError, "READ_REQUEST EV_TIMEOUT");
             return;
           } else{
             ev_once(main_socket_loop, ctx->fd, EV_READ, ctx->timeout, ReciveRequest_GetRequest, ctx);
             return;
           }
        }
  
        if(EV_READ & revents){
          err_t err = ReciveRequest_GetRequest_ReadRequest(ctx->fd, ctx->request, &ctx->req_len, ctx->req_size);        
          if(0 == err){
              ctx->life_cycle = PARSE_REQUEST;
              RegisterAayncWatcher(&ctx->async_watcher, ParserRequest, ctx);
              ev_async_start(main_socket_loop, &ctx->async_watcher);
              ev_async_send(main_socket_loop, &ctx->async_watcher);
          } else{
               if((EAGAIN == err || EWOULDBLOCK == err || EINTR == err)) {
                   ev_once(main_socket_loop, ctx->fd, EV_READ, ctx->timeout, ReciveRequest_GetRequest, ctx);
                   return;
               } else{
                   SpxLog2(g_log, SpxLogError, err,"ReciveRequest_GetRequest_ReadRequest Failed");
                   CloseCTX(ctx);
                   return;
               }
          }
        }
    }
}/*}}}*/