/* ** Send extended string to socket (dynamic webpages). ** ** IN: ** HTTPSRV_STRUCT *server - server structure. ** HTTPSRV_SESSION_STRUCT *session - session for sending. ** char *str - string to send. ** uint32_t length - length of source string. ** ** OUT: ** none ** ** Return Value: ** int - number of bytes processed. */ static uint32_t httpsrv_sendextstr(HTTPSRV_STRUCT *server, HTTPSRV_SESSION_STRUCT *session, uint32_t length) { char *src; char* t_buffer; uint32_t n; uint32_t retval; t_buffer = session->response.script_buffer; src = session->buffer.data + session->buffer.offset; n = strlen(t_buffer); if (n == 0) /* Start searching for script token. */ { uint32_t n_send; uint32_t max; uint32_t i; char token[] = "<%"; i = 0; max = length; for (n_send = 0; (n_send < max) && (token[i]); n_send++) { if (src[n_send] == token[i]) { t_buffer[i] = token[i]; i++; max = ((n_send+2) < length) ? n_send+2 : length; } else { i = 0; max = length; session->response.script_buffer[0] = 0; } } session->buffer.offset = n_send - i; retval = i; } else if (n == 1) /* There was already the less-than sign.*/ { if (*src == '%') { /* There is script token spanning over two buffers. */ t_buffer[n] = *src; retval = 1; } else { /* There was no script token, send missing less-than sign. */ send(session->sock, session->response.script_buffer, n, 0); *((uint16_t *) session->response.script_buffer) = 0; retval = 0; } } else /* Script token beginning found, find end. */ { uint32_t name_length; char fname[HTTPSRVCFG_MAX_SCRIPT_LN] = {0}; uint32_t term_length; name_length = strcspn(src, " ;%<>\r\n\t\f"); if ((src[name_length] == '%') && (src[name_length + 1] == '>')) { term_length = 2; } else { term_length = 1; } if ((name_length > 1) && (name_length < HTTPSRVCFG_MAX_SCRIPT_LN)) { HTTPSRV_SCRIPT_MSG* msg_ptr; snprintf(fname, name_length+1, "%s", src); /* Form up message for handler task and send it. */ msg_ptr = _msg_alloc(server->script_msg_pool); if (msg_ptr != NULL) { msg_ptr->header.TARGET_QID = server->script_msgq; msg_ptr->header.SOURCE_QID = server->script_msgq; msg_ptr->header.SIZE = sizeof(HTTPSRV_SCRIPT_MSG); msg_ptr->session = session; msg_ptr->type = HTTPSRV_SSI_CALLBACK; msg_ptr->name = fname; msg_ptr->ses_tid = _task_get_id(); _msgq_send(msg_ptr); /* Wait until SSI is processed. */ _task_block(); } *((uint16_t *) session->response.script_buffer) = 0; } retval = name_length + term_length; } return(retval); }
void main_task ( uint_32 parameter ) { MESSAGE_HEADER_STRUCT_PTR msg_ptr; _task_id created_task; _mqx_uint log_result; _mqx_uint event_result; _mqx_uint sem_result; _int_install_unexpected_isr(); printf("\nMQX %s\n",_mqx_version); printf("Hello from main_task().\n"); /* create the log component */ log_result = _log_create_component(); if (log_result != MQX_OK) { /* log component could not be created */ } /* endif */ /* Create the mutex component */ if (_mutex_create_component() != MQX_OK) { /* an error has been detected */ } /* create the event component */ event_result = _event_create_component(EVENT_INITIAL_NUMBER, EVENT_GROWTH, EVENT_MAXIMUM); if (event_result != MQX_OK) { /* event component could not be created */ printf("Error: Cannot create event component\n"); _task_block(); } /* endif */ /* create the semaphore component */ sem_result = _sem_create_component(SEM_INITIAL_NUMBER, SEM_GROWTH, SEM_MAXIMUM); if (sem_result != MQX_OK) { /* semaphore component could not be created */ printf("Error: Cannot create semaphore component\n"); _task_block(); } /* endif */ MsgPool_pool_id = _msgpool_create ( 8, 10, 0, 0); if (MsgPool_pool_id == MSGPOOL_NULL_POOL_ID) { /* _msgpool_create did not succeed */ printf("Error: Cannot create message pool\n"); _task_block(); } Main_Queue_qid = _msgq_open( MSGQ_FREE_QUEUE, SIZE_UNLIMITED); if (Main_Queue_qid == (_queue_id)0){ /* queue could not be opened */ printf("Error: Cannot open message pool\n"); _task_block(); } created_task = _task_create(0, SENDER, 0); if (created_task == MQX_NULL_TASK_ID) { /* task creation failed */ printf("Error: Cannot create task\n"); _task_block(); } created_task = _task_create(0, MUTEXA, 0); if (created_task == MQX_NULL_TASK_ID) { /* task creation failed */ printf("Error: Cannot create task\n"); _task_block(); } created_task = _task_create(0, MUTEXB, 0); if (created_task == MQX_NULL_TASK_ID) { /* task creation failed */ printf("Error: Cannot create task\n"); _task_block(); } created_task = _task_create(0, SEMA, 0); if (created_task == MQX_NULL_TASK_ID) { /* task creation failed */ printf("Error: Cannot create task\n"); _task_block(); } created_task = _task_create(0, SEMB, 0); if (created_task == MQX_NULL_TASK_ID) { /* task creation failed */ printf("Error: Cannot create task\n"); _task_block(); } created_task = _task_create(0, EVENTA, 0); if (created_task == MQX_NULL_TASK_ID) { /* task creation failed */ printf("Error: Cannot create task\n"); _task_block(); } created_task = _task_create(0, EVENTB, 0); if (created_task == MQX_NULL_TASK_ID) { /* task creation failed */ printf("Error: Cannot create task\n"); _task_block(); } #if MQX_KERNEL_LOGGING /* create log number 0 */ log_result = _klog_create(200, 0); if (log_result != MQX_OK) { /* log 0 could not be created */ printf("Error: Cannot create kernel log\n"); _task_block(); } /* endif */ /* define kernel logging */ _klog_control(0xFFFFFFFF, FALSE); _klog_control( KLOG_ENABLED | KLOG_FUNCTIONS_ENABLED | KLOG_INTERRUPTS_ENABLED | KLOG_SYSTEM_CLOCK_INT_ENABLED | KLOG_CONTEXT_ENABLED | KLOG_TASKING_FUNCTIONS | KLOG_ERROR_FUNCTIONS | KLOG_MESSAGE_FUNCTIONS | KLOG_INTERRUPT_FUNCTIONS | KLOG_MEMORY_FUNCTIONS | KLOG_TIME_FUNCTIONS | KLOG_EVENT_FUNCTIONS | KLOG_NAME_FUNCTIONS | KLOG_MUTEX_FUNCTIONS | KLOG_SEMAPHORE_FUNCTIONS | KLOG_WATCHDOG_FUNCTIONS, TRUE ); #endif /* ** LOOP - */ while ( TRUE ) { msg_ptr = _msg_alloc((_pool_id) MsgPool_pool_id ); if (msg_ptr == NULL) { /* No available message buffer */ } msg_ptr->SIZE = sizeof(MESSAGE_HEADER_STRUCT); msg_ptr->SOURCE_QID = msg_ptr->TARGET_QID; msg_ptr->TARGET_QID = Sender_Queue_qid; _msgq_send(msg_ptr); /* * Service the message queue - Main_Queue */ msg_ptr = _msgq_receive_ticks(Main_Queue_qid, NO_TIMEOUT); /* process message End_msg */ _msg_free(msg_ptr); } /* endwhile */ } /*end of task*/
/* ** Send extended string to socket (dynamic webpages). ** ** IN: ** HTTPSRV_STRUCT *server - server structure. ** HTTPSRV_SESSION_STRUCT *session - session for sending. ** char *str - string to send. ** uint32_t length - length of source string. ** ** OUT: ** none ** ** Return Value: ** int - number of bytes processed. */ static _mqx_int httpsrv_sendextstr(HTTPSRV_STRUCT *server, HTTPSRV_SESSION_STRUCT *session, char *str, uint32_t length) { char *src; int len, res; int add = 0; char fname[HTTPSRVCFG_MAX_SCRIPT_LN + 1]; uint32_t old_data = session->buffer.offset; src = str; fname[0] = 0; if (session->response.script_token) { // script token found len = (int)strcspn(src, " ;%<>\r\n\t"); if (len > 1 && len < HTTPSRVCFG_MAX_SCRIPT_LN) { HTTPSRV_SCRIPT_MSG* msg_ptr; snprintf(fname, len+1, "%s", src); /* Form up message for handler task and send it */ msg_ptr = _msg_alloc(server->script_msg_pool); if (msg_ptr != NULL) { msg_ptr->header.TARGET_QID = server->script_msgq; msg_ptr->header.SOURCE_QID = server->script_msgq; msg_ptr->header.SIZE = sizeof(HTTPSRV_SCRIPT_MSG); msg_ptr->session = session; msg_ptr->type = HTTPSRV_SSI_CALLBACK; msg_ptr->name = fname; msg_ptr->ses_tid = _task_get_id(); _msgq_send(msg_ptr); /* wait until SSI is processed */ _task_block(); } } if (src[len] == '%' && src[len + 1] == '>') { session->response.script_token = 0; len += 1; } len++; } else { for (len = 0; *src && len < length; src++, len++) { if (src[0] == '<' && src[1] == '%') { session->response.script_token = 1; src += 2; add = 2; break; } } res = send(session->sock, str, len, 0); session->buffer.offset = 0; _mem_zero(session->buffer.data, HTTPSRV_SES_BUF_SIZE_PRV); if (res < 0) { session->response.script_token = 0; #if MQX_USE_IO_OLD res = errno; #else res = _task_errno; #endif if (res != EAGAIN) { len = 0; } } else if (len != res) { session->response.script_token = 0; } } return ((len+add)-old_data); }