herror_t httpd_init(int argc, char **argv) { herror_t status; _httpd_parse_arguments(argc, argv); if ((status = hsocket_module_init(argc, argv)) != H_OK) { log_error("hsocket_modeule_init failed (%s)", herror_message(status)); return status; } _httpd_connection_slots_init(); if ((status = _httpd_register_builtin_services(argc, argv)) != H_OK) { log_error("_httpd_register_builtin_services failed (%s)", herror_message(status)); return status; } if ((status = hsocket_init(&_httpd_socket)) != H_OK) { log_error("hsocket_init failed (%s)", herror_message(status)); return status; } if ((status = hsocket_bind(&_httpd_socket, _httpd_port)) != H_OK) { log_error("hsocket_bind failed (%s)", herror_message(status)); return status; } return H_OK; }
/*-------------------------------------------------- FUNCTION: httpc_new DESC: Creates a new http client connection object You need to create at least 1 http client connection to communicate via http. ----------------------------------------------------*/ httpc_conn_t * httpc_new(void) { static int counter = 10000; herror_t status; httpc_conn_t *res; if (!(res = (httpc_conn_t *) malloc(sizeof(httpc_conn_t)))) return NULL; if ((status = hsocket_init(&res->sock)) != H_OK) { log_warn2("hsocket_init failed (%s)", herror_message(status)); return NULL; } res->header = NULL; res->version = HTTP_1_1; res->out = NULL; res->_dime_package_nr = 0; res->_dime_sent_bytes = 0; res->id = counter++; return res; }
MIME_read_status mime_streamreader_function(void *userdata, unsigned char *dest, int *size) { int readed = 0; http_input_stream_t *in = (http_input_stream_t *) userdata; if (!http_input_stream_is_ready(in)) return MIME_READ_EOF; readed = http_input_stream_read(in, dest, *size); /* log_info1("http_input_stream_read() returned 0"); */ if (readed == -1) { log_error4("[%d] %s():%s ", herror_code(in->err), herror_func(in->err), herror_message(in->err)); } *size = readed; if (*size != -1) { return MIME_READ_OK; } return MIME_READ_ERROR; }
void err_soap(herror_t err) { if(err==H_OK) { return; } printf ("%s():%s [%d]\n",herror_func(err), herror_message(err), herror_code(err)); herror_release (err); }
int main(int argc, char *argv[]) { SoapCtx *req, *resp; herror_t err; err = soap_client_init_args(argc, argv); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } err = soap_ctx_new_with_method(urn, "computePi", &req); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } soap_env_add_item(req->env, "xsd:decimal", "precision", "10000"); if (argc > 1) err = soap_client_invoke(req, &resp, argv[1], ""); else err = soap_client_invoke(req, &resp, url, ""); if (err != H_OK) { log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(req); return 1; } soap_xml_doc_print(resp->env->root->doc); soap_ctx_free(resp); soap_ctx_free(req); soap_client_destroy(); return 0; }
static herror_t _httpd_register_builtin_services(int argc, char **argv) { herror_t status; if ((status = httpd_admin_init_args(argc, argv)) != H_OK) { log_error("httpd_admin_init_args failed (%s)", herror_message(status)); return status; } return H_OK; }
static int send_file(httpc_conn_t *conn, const char* filename, const char* id, const char* content_type) { herror_t status; int size; FILE *f = fopen(filename, "r"); char buffer[MAX_BUFFER_SIZE]; if ((f = fopen(filename, "r")) == NULL) { fprintf(stderr, "cannot open file: '%s' (%s)\n", filename, strerror(errno)); return -1; } if ((status = httpc_mime_next(conn, id, content_type, "binary")) != H_OK) { fprintf(stderr, "httpc_mime_next failed (%s)\n", herror_message(status)); herror_release(status); return -1; } while (!feof(f)) { size = fread(buffer, 1, MAX_BUFFER_SIZE, f); if (size == -1) { fprintf(stderr, "Cannot read file (%s)\n", strerror(errno)); fclose(f); return -1; } http_output_stream_write(conn->out, buffer, size); } fclose(f); return 0; }
static void * httpd_session_main(void *data) #endif { struct hrequest_t *req; conndata_t *conn; httpd_conn_t *rconn; hservice_t *service; herror_t status; struct timeval start, end, duration; int done; if (gettimeofday(&start, NULL) == -1) log_error("gettimeofday failed (%s)", strerror(errno)); conn = (conndata_t *) data; log_verbose("starting new httpd session on socket %d", conn->sock); rconn = httpd_new(&(conn->sock)); done = 0; while (!done) { log_verbose("starting HTTP request on socket %d (%p)", conn->sock, conn->sock.sock); if ((status = hrequest_new_from_socket(&(conn->sock), &req)) != H_OK) { int code; switch ((code = herror_code(status))) { case HSSL_ERROR_SSLCLOSE: case HSOCKET_ERROR_RECEIVE: log_error("hrequest_new_from_socket failed (%s)", herror_message(status)); break; default: httpd_send_bad_request(rconn, herror_message(status)); break; } herror_release(status); done = 1; } else { char *conn_str; _httpd_request_print(req); conn_str = hpairnode_get_ignore_case(req->header, HEADER_CONNECTION); if (conn_str && strncasecmp(conn_str, "close", 6) == 0) done = 1; if (!done) done = req->version == HTTP_1_0 ? 1 : 0; if ((service = httpd_find_service(req->path))) { log_verbose("service '%s' for '%s' found", service->context, req->path); if (service->status == NHTTPD_SERVICE_UP) { pthread_rwlock_wrlock(&(service->statistics->lock)); service->statistics->requests++; pthread_rwlock_unlock(&(service->statistics->lock)); if (_httpd_authenticate_request(req, service->auth)) { if (service->func != NULL) { service->func(rconn, req); if (gettimeofday(&end, NULL) == -1) log_error("gettimeofday failed (%s)", strerror(errno)); timersub(&end, &start, &duration); pthread_rwlock_wrlock(&(service->statistics->lock)); service->statistics->bytes_received += rconn->sock->bytes_received; service->statistics->bytes_transmitted += rconn->sock->bytes_transmitted; timeradd(&(service->statistics->time), &duration, &(service->statistics->time)); pthread_rwlock_unlock(&(service->statistics->lock)); if (rconn->out && rconn->out->type == HTTP_TRANSFER_CONNECTION_CLOSE) { log_verbose("Connection close requested"); done = 1; } } else { char buffer[256]; snprintf(buffer, 256, "service '%s' is not registered properly (service function is NULL)", req->path); log_verbose("%s", buffer); httpd_send_not_implemented(rconn, buffer); } } else { httpd_send_unauthorized(rconn, req->path); done = 1; } } else { char buffer[256]; sprintf(buffer, "service for '%s' is disabled", req->path); log_verbose("%s", buffer); httpd_send_internal_error(rconn, buffer); } } else { char buffer[256]; sprintf(buffer, "no service for '%s' found", req->path); log_verbose("%s", buffer); httpd_send_not_implemented(rconn, buffer); done = 1; } hrequest_free(req); } } httpd_free(rconn); hsocket_close(&(conn->sock)); #ifdef WIN32 CloseHandle((HANDLE) conn->tid); #else pthread_attr_destroy(&(conn->attr)); #endif conn->flag = CONNECTION_FREE; #ifdef WIN32 _endthread(); return 0; #else /* pthread_exits automagically */ return NULL; #endif }
herror_t httpd_run(void) { struct timeval timeout; conndata_t *conn; herror_t err; fd_set fds; log_verbose("starting run routine"); _httpd_register_signal_handler(); if ((err = hsocket_listen(&_httpd_socket)) != H_OK) { log_error("hsocket_listen failed (%s)", herror_message(err)); return err; } while (_httpd_run) { conn = _httpd_wait_for_empty_conn(); if (!_httpd_run) break; /* Wait for a socket to accept */ while (_httpd_run) { /* set struct timeval to the proper timeout */ timeout.tv_sec = 1; timeout.tv_usec = 0; /* zero and set file descriptior */ FD_ZERO(&fds); FD_SET(_httpd_socket.sock, &fds); /* select socket descriptor */ switch (select(_httpd_socket.sock + 1, &fds, NULL, NULL, &timeout)) { case 0: /* descriptor is not ready */ continue; case -1: /* got a signal? */ continue; default: /* no nothing */ break; } if (FD_ISSET(_httpd_socket.sock, &fds)) { break; } } /* check signal status */ if (!_httpd_run) break; if ((err = hsocket_accept(&_httpd_socket, &(conn->sock))) != H_OK) { log_error("hsocket_accept failed (%s)", herror_message(err)); hsocket_close(&(conn->sock)); continue; } _httpd_start_thread(conn); } return 0; }
int main(int argc, char **argv) { httpc_conn_t *conn; /* Client connection object */ hresponse_t *res; /* Response object **/ herror_t status; FILE *f; size_t size; char url[50], file[50], id[50], content_type[50]; char buffer[MAX_BUFFER_SIZE+1]; /* Check usage */ if (argc < 5) { fprintf(stderr, "usage %s <url> <file> <id> <content-type>\n", argv[0]); exit(1); } /* Set log level to see more information written by the library */ // log_set_level(NANOHTTP_LOG_VERBOSE); /* Initialize httpc module */ if ((status = httpc_init(argc, argv)) != H_OK) { fprintf(stderr, "Cannot init httpc (%s)\n", herror_message(status)); herror_release(status); return 1; } /* Create the client connection object */ conn = httpc_new(); httpc_set_header(conn, HEADER_TRANSFER_ENCODING, TRANSFER_ENCODING_CHUNKED); /* Open connection for mime */ if ((status = httpc_mime_begin(conn, argv[1], argv[3], "", argv[4])) != H_OK) { fprintf(stderr, "Can not start MIME: %s\n", herror_message(status)); herror_release(status); exit(1); } if (!send_file(conn, argv[2], argv[3], argv[4])) { fprintf(stderr, "send_file failed\n"); exit(1); } while (1) { printf("Enter filename ['.' for finish]: "); gets(file); if (!strcmp(file, ".")) break; printf("Enter part id:"); gets(id); printf("Enter content-type:"); gets(content_type); if (!send_file(conn, file, id, content_type)) exit(1); } if ((status = httpc_mime_end(conn, &res)) != H_OK) { fprintf(stderr, "httpc_mime_end failed (%s)\n", herror_message(status)); herror_release(status); exit(1); } /* Show response */ show_response(res); /* clean up*/ hresponse_free(res); httpc_free(conn); return 0; }
int main(int argc, char *argv[]) { SoapCtx *ctx, *ctx2; herror_t err; char *url,*method,*arg; int i; /* check the command line parameters */ if(argc < 3) { printusage(argv[0]); return 1; } arg = argv[0]; strcpy(startme,arg); for(i=1; i<argc; i++) { arg = argv[i]; strcat(startme," "); strcat(startme,arg); } url = argv[1]; method = argv[2]; for(i=3; i<argc; i++) { arg = argv[i]; if(strlen(arg) <= MAX_PATH_LENGTH) { if(strncmp(arg,"-itemlist=",10) == 0) { sscanf(arg,"-itemlist=%s",itemlist); } if(strncmp(arg,"-shm=",5) == 0) { sscanf(arg,"-shm=%s",shm); } if(strncmp(arg,"-mbx=",5) == 0) { sscanf(arg,"-mbx=%s",mbx); } if(strncmp(arg,"-sleep=",7) == 0) { sscanf(arg,"-sleep=%d",&sleep); if(sleep < 10) sleep = 10; } if(strncmp(arg,"-max_name_length=",17) == 0) { sscanf(arg,"-max_name_length=%d",&max_name_length); if(max_name_length < 31) max_name_length = 31; } if(strncmp(arg,"-shmsize=",8) == 0) { sscanf(arg,"-shmsize=%ld",&shmsize); if(shmsize < 1) shmsize = 1; } if(strcmp(arg,"-debug") == 0) { debug = 1; } } else { printf("%s is too long.\n", arg); return 1; } } /* init cSOAP client */ err = soap_client_init_args(argc, argv); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } if(strcmp(method,"GetStatus") == 0) { /* create a SoapCtx object */ printf("soap_ctx_new_with_method(%s,\"GetStatus\")\n", URN); err = soap_ctx_new_with_method(URN, "GetStatus", &ctx); if (err != H_OK) { printf("err != H_OK\n"); log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } /* invoke */ printf("soap_client_invoke(\"%s\")\n", url); err = soap_client_invoke(ctx, &ctx2, url, ""); if (err != H_OK) { printf("err != H_OK\n"); log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(ctx); return 1; } /* print the result */ soap_xml_doc_print(ctx2->env->root->doc); /* free the objects */ printf("soap_ctx_free()\n"); soap_ctx_free(ctx2); soap_ctx_free(ctx); /* destroy the cSOAP client */ printf("soap_client_destroy()\n"); soap_client_destroy(); return 0; } else if(strcmp(method,"Browse") == 0) { printf("#%s %s %s\n",argv[0],url,method); printf("#\n"); /* create a SoapCtx object */ err = soap_ctx_new_with_method(URN, "Browse", &ctx); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } /* invoke */ err = soap_client_invoke(ctx, &ctx2, url, ""); if (err != H_OK) { log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(ctx); return 1; } /* browse the childs */ browse_childs(url, ctx2->env->cur->children); /* free the objects */ soap_ctx_free(ctx2); soap_ctx_free(ctx); /* destroy the cSOAP client */ soap_client_destroy(); return 0; } else if(strcmp(method,"Run") == 0) { i = getMaxItemNameLength(); if(i > 0) i = run(url,i); soap_client_destroy(); return i; } else { printusage(argv[0]); soap_client_destroy(); } return 0; }
int browse_childs(char *url, xmlNodePtr node) { SoapCtx *ctx, *ctx2; herror_t err; const char *itemname, *isitem, *haschildren; while(node != NULL) { if(strcmp((const char *) node->name, "Elements") == 0) { itemname = (const char *) xmlGetProp(node, (xmlChar*) "ItemName"); isitem = (const char *) xmlGetProp(node, (xmlChar*) "IsItem"); haschildren = (const char *) xmlGetProp(node, (xmlChar*) "HasChildren"); if(haschildren != NULL && strcmp(haschildren,"true") == 0) { if(itemname != NULL) { /* browse childs */ printf("#\n"); printf("#%s\n",itemname); /* create a SoapCtx object */ err = soap_ctx_new_with_method(URN, "Browse", &ctx); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return -1; } /* add to the request */ soap_env_add_item(ctx->env, "xsd:string", "ItemName", itemname); /* invoke */ err = soap_client_invoke(ctx, &ctx2, url, ""); if (err != H_OK) { log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(ctx); return -1; } /* print the result */ //soap_xml_doc_print(ctx2->env->root->doc); /* browse the childs */ browse_childs(url, ctx2->env->cur->children); /* free the objects */ soap_ctx_free(ctx2); soap_ctx_free(ctx); } } else if(haschildren != NULL && strcmp(haschildren,"false") == 0 && isitem != NULL && strcmp(isitem,"true") == 0) { printf("%s\n", itemname); } } node = node->next; } return 0; }
int run(const char *url, int maxItemNameLength) { long shmsize_needed = ((maxItemNameLength+1)+(max_name_length+1))*num_items + sizeof(SHM_HEADER); FILE *fin; char buf[1024], *cptr; void *shmadr; SHM_HEADER *shmheader; int i; SoapCtx *ctx, *ctx2; xmlNodePtr xmlcur,xmlitem; herror_t err; if(url == NULL) return 1; // print shm parameters printf("maxItemNameLength=%d max_name_length=%d shmsize=%ld shmsize_needed=%ld num_items=%d shm=%s\n", maxItemNameLength, max_name_length, shmsize, shmsize_needed, num_items, shm); if(shmsize_needed > shmsize) { printf("shmsize too small -> increase it\n"); return 1; } if(maxItemNameLength <= 0 || max_name_length <= 0 || shmsize <= 0 || shmsize_needed <= 0) { printf("some values are negative or 0\n"); return 1; } if(maxItemNameLength >= (int) (sizeof(buf) - 100) || max_name_length >= (int) (sizeof(buf) - 100)) { printf("name is bigger than buf length = %d\n", (int) sizeof(buf)); return 1; } // init shared memory rlSharedMemory rlshm = rlSharedMemory(shm, (unsigned long) shmsize); if(rlshm.status != rlSharedMemory::OK) { printf("shared memory status is not OK\n"); return 1; } shmadr = rlshm.getUserAdr(); if(shmadr == NULL) { printf("shmadr = NULL\n"); return 1; } memset(shmadr,0,shmsize); // read itemlist to shared memory fin = fopen(itemlist,"r"); if(fin == NULL) { printf("could not open itemlist %s\n",itemlist); return 1; } i = 0; while(fgets(buf,sizeof(buf)-1,fin) != NULL) { if(buf[0] > ' ' && buf[0] != '#') { cptr = strchr(buf,'\n'); if(cptr != NULL) *cptr = '\0'; cptr = (char *) shmadr; cptr += sizeof(SHM_HEADER) + (i*(maxItemNameLength+1 + max_name_length+1)); strcpy(cptr,buf); i++; } } fclose(fin); // init header in shared memory shmheader = (SHM_HEADER *) shmadr; shmheader->maxItemNameLength = maxItemNameLength; shmheader->maxNameLength = max_name_length; shmheader->numItems = num_items; shmheader->readErrorCount = 0; shmheader->writeErrorCount = 0; strcpy(shmheader->ident,"opc"); /* create a SoapCtx object */ err = soap_ctx_new_with_method(URN, "Read", &ctx); if(err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); return 1; } /* create the Read ItemList */ xmlitem = soap_env_add_item(ctx->env, "xsd:element", "ItemList", ""); xmlcur = ctx->env->cur; ctx->env->cur = xmlitem; for(i=0; i<shmheader->numItems; i++) { cptr = (char *) shmadr; cptr += sizeof(SHM_HEADER) + (i*(maxItemNameLength+1 + max_name_length+1)); sprintf(buf,"Items ItemName=\"%s\"",cptr); soap_env_add_item(ctx->env, "xsd:string", buf, NULL); } ctx->env->cur = xmlcur; // create reader thread and the watchdog rlThread reader,watchdog; reader.create(reader_thread,(void *) url); watchdog.create(watchdog_thread,NULL); // poll the OPC XML-DA server forever while(1) { /* invoke */ err = soap_client_invoke(ctx, &ctx2, url, ""); if(err == H_OK) { /* print the result */ if(debug) soap_xml_doc_print(ctx2->env->root->doc); /* write the result to the shared memory */ reader.lock(); write_to_shared_memory(ctx2,shmadr); reader.unlock(); /* free the objects */ soap_ctx_free(ctx2); } watchcnt1++; if(watchcnt1 > 256*256) watchcnt1 = 0; rlsleep(sleep); } return 0; }
int main( int argc, char** argv ) { SoapCtx* request; SoapCtx* response; herror_t error; int ii; /* The array of parameter values to be fetched from argv. */ char* parameters[Parameter_Num_Pos]; xmlChar* buildID; if ( argc != Parameter_Num_Pos + 1 ) { printf( "Regresstor: Invalid Number of arguments! Num given: %d\n", argc ); printUsage(); return EXIT_FAILURE; } initParametersArray( parameters ); fetchParametersFromArgv( parameters, argc, argv ); if ( ! checkParameters( parameters ) ) { printUsage(); return EXIT_FAILURE; } printf( "Regresstor: calling web service %s(), %s at %s.\n", Regresstor_SubmitCheckMethod, Regresstor_URN, parameters[URL_Pos]); /* SOAP CALL */ error = soap_client_init_args( argc, argv ); if ( error != H_OK ) { log_error4( "%s():%s [%d]", herror_func(error), herror_message(error), herror_code(error) ); herror_release(error); return EXIT_FAILURE; } error = soap_ctx_new_with_method( Regresstor_URN, Regresstor_SubmitCheckMethod, &request ); if ( error != H_OK ) { log_error4( "%s():%s [%d]", herror_func(error), herror_message(error), herror_code(error) ); herror_release(error); return EXIT_FAILURE; } /* Add parameters into envelope except for the URL */ soap_env_push_item( request->env, NULL, "parameters" ); for ( ii = 0; ii < URL_Pos; ++ii ) { if ( ii == OutputLocation_Pos ) { /* Attach file */ char* contents; char* paramName = NULL; int sendDummyFile = 1; paramName = "Output"; if ( strcmp( parameters[ii], "none" ) != 0 ) { contents = getFileContents( parameters[ii] ); if ( contents != NULL ) { soap_env_add_item( request->env, "xsd:string", paramName, contents ); free( contents ); sendDummyFile = 0; } } if ( sendDummyFile ) { soap_env_add_item( request->env, "xsd:string", paramName, "No output file" ); } } else { soap_env_add_item( request->env, "xsd:string", parameterNames[ii], parameters[ii] ); } } /* Check commandline to see if diff passed. If fail, add a "Diff Failed" sub test */ if ( strcmp( parameters[Passed_Pos], "0" ) == 0 ) { /* Add the sub test */ soap_env_add_item( request->env, "xsd:string", "FailedSubTests", "Error failure" ); } soap_env_pop_item( request->env ); error = soap_client_invoke( request, &response, parameters[URL_Pos], ""); if ( error != H_OK ) { log_error4( "[%d] %s(): %s ", herror_code(error), herror_func(error), herror_message(error) ); herror_release( error ); soap_ctx_free( request ); return EXIT_FAILURE; } interpretResponse( response, &buildID ); soap_ctx_free( request ); soap_ctx_free( response ); soap_client_destroy(); return EXIT_SUCCESS; }
u_int32_t ws_umc_call(WS_ENV* ws_env, WS_REQ_RES* ws_req_res, char* method, char* end_point, char** reason) { SoapCtx *ctx = NULL, *ctx2 = NULL; herror_t err = NULL; xmlNodePtr node; s8 url[SOAP_UMC_URL_LEN]; s32 ret = 0; s8* fault_string; s8 umc_addr[24] = {0}; s32 umc_port; s8 umc_url[30] = {0}; /*soap client*/ err = soap_client_init_args(0, NULL); if (err != H_OK) { ret = ERROR_FAIL; goto ret_label; } /*为soap client添加方法*/ err = soap_ctx_new_with_method(SOAP_UMC_URN, method, &ctx); if (err != H_OK) { ret = ERROR_FAIL; goto ret_label; } /*为soap client添加请求参数*/ ret = ws_iterate_req(ws_env, ws_req_res, ws_add_para_tosoap, (void *)ctx); if(ret) { goto ret_label; } ret = umc_getip(umc_addr, sizeof(umc_addr), &umc_port); if(ret) { goto ret_label; } if(umc_port == 0) { umc_port = 80; } snprintf(umc_url, sizeof(umc_url), "%s:%d", umc_addr, umc_port); snprintf(url, SOAP_UMC_URL_LEN, "http://%s/%s", umc_url, end_point+strlen("::/")); /*建立客户端, 发送请求*/ err = soap_client_invoke_auth(ctx, &ctx2, url, "", "admin", "admin", 0); if (err != H_OK) { ret = ERROR_FAIL; goto ret_label; } /*解析收到的soap应答,知道找到ret退出*/ for (node = ctx2->env->root; node; ) { if (!xmlStrcmp(node->name, BAD_CAST "Header")) { node = soap_xml_get_next(node); } else if (!xmlStrcmp(node->name, BAD_CAST "ret") /*|| !xmlStrstr(node->name, BAD_CAST ":ret")*/) { (void)ws_set_res_type(ws_env, ws_req_res, WS_RESULT_STRUCT); node = soap_xml_get_children(node); break; } else { node = soap_xml_get_children(node); } } /*此时节点指向 ret的children节点,*/ while (node) { if (!xmlStrcmp(node->name, BAD_CAST "res_count")) { /*将总数添加res结构中*/ ret = ws_add_res_int(ws_env, ws_req_res, (s8 *)(node->name), atoi(soap_xml_get_text(node))); if(ret) { break; } /*设置类型*/ ws_set_res_type_struct_list(ws_env, ws_req_res, atoi(soap_xml_get_text(node))); } else if (!xmlStrcmp(node->name, BAD_CAST "umc_res")) { node = soap_xml_get_children(node); continue; } else if (!xmlStrcmp(node->name, BAD_CAST "res")) { /*添加一行,每个res占用一行, 实际 为指针后移一行最大数*/ ws_res_new_struct_list(ws_env, ws_req_res); /*添加一个res内的数据*/ ret = umc_get_res(ws_env, ws_req_res, node); if(ret) { break; } } else { /*结构类型res的添加*/ ret = ws_add_res_string(ws_env, ws_req_res, (s8 *)(node->name), soap_xml_get_text(node)); if(ret) { break; } } node = soap_xml_get_next(node); } ret_label: if(ret != 0) { if(err != H_OK) { fault_string = herror_message(err); } else { fault_string = "umc get system error"; } *reason = (char*)ws_strdup(ws_env, fault_string); } herror_release(err); soap_ctx_free(ctx2); soap_ctx_free(ctx); soap_client_destroy(); return ret; }
void *reader_thread(void *arg) { SoapCtx *ctx, *ctx2; xmlNodePtr xmlcur,xmlitem,xmlitem2; herror_t err; char *itemname,*itemvalue,buf[1024], *cptr; int ret; THREAD_PARAM *p = (THREAD_PARAM *) arg; const char *url = (const char *) p->user; rlMailbox rlmbx(mbx); // read mbx until it is empty rlmbx.clear(); printf("reader_thread starting\n"); // wait for commands from clients while(1) { ret = rlmbx.read(buf,sizeof(buf)); // read "itemname,itemvalue\n" if(ret <= 0) continue; itemname = itemvalue = &buf[0]; cptr = strchr(buf,','); if(cptr != NULL) { *cptr = '\0'; cptr++; itemvalue = cptr; cptr = strchr(itemvalue,'\n'); if(cptr != NULL) *cptr = 0; } if(debug) { printf("reader_thread Write itemname=%s itemvalue=%s\n",itemname,itemvalue); } p->thread->lock(); /* create a SoapCtx object */ err = soap_ctx_new_with_method(URN, "Write", &ctx); if (err != H_OK) { log_error4("%s():%s [%d]", herror_func(err), herror_message(err), herror_code(err)); herror_release(err); goto end_of_while; } /* create the ItemList */ xmlitem = soap_env_add_item(ctx->env, "xsd:element", "ItemList", ""); xmlcur = ctx->env->cur; ctx->env->cur = xmlitem; xmlitem2 = soap_env_add_item(ctx->env, "xsd:string", "Items", NULL); if (!xmlNewProp(xmlitem2, BAD_CAST "ItemName", BAD_CAST itemname)) { log_error1("Can not create new xml attribute ItemName"); goto end_of_while; } ctx->env->cur = xmlitem2; if(isdigit(itemvalue[0])) { soap_env_add_item(ctx->env,"xsd:double","Value",itemvalue); } else if(strcmp(itemvalue, "true") == 0 || strcmp(itemvalue, "false") == 0) { soap_env_add_item(ctx->env, "xsd:boolean", "Value", itemvalue); } else { soap_env_add_item(ctx->env,"xsd:istring","Value",itemvalue); } ctx->env->cur = xmlitem; ctx->env->cur = xmlcur; /* invoke */ err = soap_client_invoke(ctx, &ctx2, url, ""); if (err != H_OK) { log_error4("[%d] %s(): %s ", herror_code(err), herror_func(err), herror_message(err)); herror_release(err); soap_ctx_free(ctx); goto end_of_while; } /* print the result */ if(debug) { printf("reader_thread result:\n"); soap_xml_doc_print(ctx2->env->root->doc); } end_of_while: /* free the objects */ soap_ctx_free(ctx2); soap_ctx_free(ctx); p->thread->unlock(); } return NULL; }
int main(int argc, char **argv) { herror_t status; nanohttp_log_set_loglevel(NANOHTTP_LOG_INFO); if (httpd_init(argc, argv)) { fprintf(stderr, "Cannot init httpd\n"); exit(1); } if ((status = httpd_register("/", root_service)) != H_OK) { fprintf(stderr, "Cannot register service (%s)\n", herror_message(status)); herror_release(status); httpd_destroy(); exit(1); } if ((status = httpd_register_secure("/secure", secure_service, simple_authenticator)) != H_OK) { fprintf(stderr, "Cannot register secure service (%s)\n", herror_message(status)); herror_release(status); httpd_destroy(); exit(1); } if ((status = httpd_register("/headers", headers_service)) != H_OK) { fprintf(stderr, "Cannot register headers service (%s)\n", herror_message(status)); herror_release(status); httpd_destroy(); exit(1); } if ((status = httpd_register("/mime", mime_service)) != H_OK) { fprintf(stderr, "Cannot register MIME service (%s)\n", herror_message(status)); herror_release(status); httpd_destroy(); exit(1); } if ((status = httpd_register("/post", post_service)) != H_OK) { fprintf(stderr, "Cannot register POST service (%s)\n", herror_message(status)); herror_release(status); httpd_destroy(); exit(1); } if ((status = httpd_register_default("/error", default_service)) != H_OK) { fprintf(stderr, "Cannot register default service (%s)\n", herror_message(status)); herror_release(status); httpd_destroy(); exit(1); } if ((status = httpd_run()) != H_OK) { fprintf(stderr, "Cannot run httpd (%s)\n", herror_message(status)); herror_release(status); httpd_destroy(); exit(1); } httpd_destroy(); return 0; }
/*-------------------------------------------------- FUNCTION: httpc_talk_to_server DESC: This function is the heart of the httpc module. It will send the request and process the response. Here the parameters: method: the request method. This can be HTTP_REQUEST_POST and HTTP_REQUEST_GET. conn: the connection object (created with httpc_new()) urlstr: the complete url in string format. http://<host>:<port>/<context> where <port> is not mendatory. start_cb: a callback function, which will be called when the response header is completely arrives. cb: a callback function, which will be called everytime when data arrives. content_size: size of content to send. (only if method is HTTP_REQUEST_POST) content: the content data to send. (only if method is HTTP_REQUEST_POST) userdata: a user define data, which will be passed to the start_cb and cb callbacks as a parameter. This can also be NULL. If success, this function will return 0. >0 otherwise. ----------------------------------------------------*/ static herror_t httpc_talk_to_server(hreq_method_t method, httpc_conn_t * conn, const char *urlstr) { hurl_t url; char buffer[4096]; herror_t status; int ssl; if (conn == NULL) { return herror_new("httpc_talk_to_server", GENERAL_INVALID_PARAM, "httpc_conn_t param is NULL"); } /* Build request header */ httpc_header_set_date(conn); if ((status = hurl_parse(&url, urlstr)) != H_OK) { log_error2("Can not parse URL '%s'", SAVE_STR(urlstr)); return status; } /* TODO (#1#): Check for HTTP protocol in URL */ /* Set hostname */ httpc_set_header(conn, HEADER_HOST, url.host); ssl = url.protocol == PROTOCOL_HTTPS ? 1 : 0; /* Open connection */ if ((status = hsocket_open(&conn->sock, url.host, url.port, ssl)) != H_OK) return status; switch(method) { case HTTP_REQUEST_GET: sprintf(buffer, "GET %s HTTP/%s\r\n", (url.context[0] != '\0') ? url.context : ("/"), (conn->version == HTTP_1_0) ? "1.0" : "1.1"); break; case HTTP_REQUEST_POST: sprintf(buffer, "POST %s HTTP/%s\r\n", (url.context[0] != '\0') ? url.context : ("/"), (conn->version == HTTP_1_0) ? "1.0" : "1.1"); break; default: log_error1("Unknown method type!"); return herror_new("httpc_talk_to_server", GENERAL_INVALID_PARAM, "hreq_method_t must be HTTP_REQUEST_GET or HTTP_REQUEST_POST"); } log_verbose1("Sending request..."); if ((status = hsocket_send(&(conn->sock), buffer)) != H_OK) { log_error2("Cannot send request (%s)", herror_message(status)); hsocket_close(&(conn->sock)); return status; } log_verbose1("Sending header..."); if ((status = httpc_send_header(conn)) != H_OK) { log_error2("Cannot send header (%s)", herror_message(status)); hsocket_close(&(conn->sock)); return status; } return H_OK; }