Пример #1
0
static void httpapp_send_all_usage( tcp_sock sock )
{
    str_t content = { 0, 0 };
    str_t content_type = MAKE_STRING( "application/x-json" );
    user_t * u = 0;

    while( (u = get_next_user(u)) != 0 )
    {
        str_t usage = httpapp_user_usage(u, get_next_user(u) != 0);
        append_string( &content, &usage );
        free( usage.str );
    }

    httpserv_send_content(sock, content_type, content, 1, 0 );
    log_printf( "200 OK %d bytes\n", content.len );
}
Пример #2
0
static void httpapp_send_user_bindings( tcp_sock sock )
{
    str_t content = { 0, 0 };
    str_t content_type = MAKE_STRING( "application/x-json" );

    user_t * u = 0;

    while( 0 != (u = get_next_user(u) ))
    {
        str_t binding = httpapp_user_binding(u, get_next_user(u) != 0);
        append_string( &content, &binding );
        free( binding.str );
    }

    httpserv_send_content(sock, content_type, content, 1, 0);
    log_printf( "200 OK %d bytes\n", content.len );
}
Пример #3
0
static void httpapp_get_csv( tcp_sock sock )
{
    str_t content = { 0, 0 };
    str_t content_type = MAKE_STRING( "text/csv" );

    user_t * u = 0;

    str_t a = MAKE_STRING( "name,current_usage,last_usage\n" );
    append_string( &content, &a );

    while( 0 != ( u = get_next_user(u) ) )
    {
        char sz[128];
        str_t temp = { 0, 0 };
        temp.str = sz;
        temp.len = sprintf( sz, "%s,%I64u,%I64u\n",
                            u->name, u->credit, u->last_credit );
        append_string( &content, &temp );
    }

    httpserv_send_content( sock, content_type, content, 1, 0 );
    log_printf( "200 OK %d bytes" );
}
Пример #4
0
/*
 *   Create response
 * ---------------------------------------------------------------------
 * - forms the response and fill it (the buffer) with sedignated data
 */
int form_response(char * in, char * out)
{
  char * endptr = &in[7];
  struct passwd * pw = NULL;
  uid_t uid = 0;
  char uname[BUFSIZE];
  char * uname_ptr = &uname[0];
  char integer[sizeof(int)];

#ifdef __linux__
  while (*endptr != '\0')
#else
  while (in[0] == 'u' ? *endptr != '\0': endptr != NULL)
#endif
  {
    pw = get_next_user(&endptr, in[0] == 'u', in[0] == 'u' ? (void *) &uid : &uname_ptr);
    if (pw == NULL)
    {
      strcat(out, "\fChyba: neznamy login ");
      if(in[0] == 'u')
      {
        sprintf(integer, "%d", uid);
        strcat(out, integer);
      }
      else
        strcat(out, uname_ptr);
      strcat(out, "\n\f");
    }
    else
    {
      get_data_for_user(out, pw, &in[1]);
      strcat(out, "\n");
    }
  }
  return EXIT_SUCCESS;
}
Пример #5
0
void process_command(char *input, char *output) {
	char *message = (char *)malloc(sizeof(line));
	WIN_OBJ window;

	int type_result;
	int user = -1;
	int from_user = -1;
	int type = -1;
	int offset = 0;
	int user_num = 0;
	int i = 0;
	UR_OBJ user_list[20];
	UR_OBJ curr_user;
	
	//should there be a function to get all this information?
	type_result = get_type_from_message(input);
	user = get_user_from_message(input);
	
	//printf("type result %d\n",type_result);
	switch(type_result) {
		case CMD_TEXT:
			get_text_from_message(input,message);
			message[strlen(message)-1] = '\0'; //remove the newline 
			type = get_text_type_from_message(input);

			if(type == TEXT_IM) {
				from_user = get_from_user_from_message(input);
				sprintf(output,"cmd:%s(%d) text:%d uid:%d fuid:%d content:%s",cmd_type[type_result], type_result, type, user, from_user, message);
			} else {
				sprintf(output,"cmd:%s(%d) text:%d uid:%d content:%s",cmd_type[type_result], type_result, type, user, message);
			}

		break;
		case	CMD_WINDOW:
			window = (WIN_OBJ)malloc(sizeof(struct window_obj));
			
			get_window_from_message(input, window);
			
			sprintf(output,"cmd:%s(%d) type:%d wid:%d x:%d y:%d z:%d w:%d h:%d",cmd_type[type_result], type_result, window->type, window->wid, window->x, window->y, window->z, window->w, window->h);
		
			free(window);
		break;
		case	CMD_VOTE:
			from_user = get_voted_for_uid_from_message(input);
			type = get_vote_type_from_message(input);
			sprintf(output,"cmd:%s(%d) type:%d uid:%d vuid:%d",cmd_type[type_result], type_result, type, user, from_user);
		break;
		case	CMD_USERLIST:
			type = get_userlist_type_from_message(input);
			
			if(type != USER_LIST_REQUEST) {
				char temp_buff[1024];
				sprintf(output,"cmd:%s(%d) type:%d uid:%d",cmd_type[type_result], type_result, type, user);
				user_list[user_num] = (UR_OBJ)malloc(sizeof(struct user_obj));

				offset = get_first_user(input, user_list[user_num]);	
			
			//	user_num++;
				
				do {	
					user_num++;
					user_list[user_num] = (UR_OBJ)malloc(sizeof(struct user_obj));
				}
				while((offset = get_next_user(offset,input, user_list[user_num])) > 0);
			
				//printf("user num %d\n",user_num);
				for(i=0; i<user_num; i++) {
					curr_user = user_list[i];
					
					sprintf(temp_buff," offset:%d uid:%d name:%s", (int)strlen(curr_user->name)+3,curr_user->uid, curr_user->name);
					strcat(output, temp_buff);
					
					free(curr_user); //we can get rid of the user since they aren't needed anymore
				}
			
			} else {
				sprintf(output,"cmd:%s(%d) type:%d uid:%d",cmd_type[type_result], type_result, type, user);
			}
		break;
		case	CMD_ERROR:
			type = get_error_type_from_message(input);
			sprintf(output,"cmd:%s(%d) type:%d",cmd_type[type_result], type_result, type);
		break;
		default:
			sprintf(output,"invalid command [%d]",type_result);
		break;
	}

	free(message);
	return;
}