static HashSet * frt_get_fields(VALUE rfields) { VALUE rval; HashSet *fields; char *s, *p, *str; if (rfields == Qnil) return NULL; fields = hs_new_str(&free); if (TYPE(rfields) == T_ARRAY) { int i; for (i = 0; i < RARRAY(rfields)->len; i++) { rval = rb_obj_as_string(RARRAY(rfields)->ptr[i]); hs_add(fields, nstrdup(rval)); } } else { rval = rb_obj_as_string(rfields); if (strcmp("*", rs2s(rval)) == 0) { hs_destroy(fields); fields = NULL; } else { s = str = nstrdup(rval); while ((p = strchr(s, '|')) != '\0') { *p = '\0'; hs_add(fields, estrdup(s)); s = p + 1; } hs_add(fields, estrdup(s)); free(str); } } return fields; }
/* * Find the application's directory locations. * As iiab based apps can be tree based or linux location based * (RPM standards), this routine finds where the important locations * are. Sets the following globals:- * iiab_dir_etc - config directory * iiab_dir_bin - executables * iiab_dir_lib - library files * iiab_dir_var - data files * iiab_dir_launch - launch directory * Called by iiab_start(), this finction can also be called * explicitly, before iiab_start(), to find the values of directories early. * when called for the second or subsequent time, the values will not * be overwritten. To reread, iiab_free_dir_locations() should be called * before hand. */ void iiab_dir_locations(char *argv0) { char cwd[PATH_MAX], **path, *eot; int stdplaces=0; if (iiab_dir_bin) /* don't reinitialise */ return; /* get cwd as launch directory */ getcwd(cwd, PATH_MAX); /* find absolute location of executable */ iiab_dir_bin = iiab_getbinpath(argv0); eot = strrchr(iiab_dir_bin, '/'); /* bin dir */ if (eot) *eot = '\0'; /* just dir part: chop at '/' */ /* if executable is in a standard place (/usr/local/bin, /bin, /usr/bin, * /sbin, /usr/sbin) then standard locations are assumed for the * support directories */ for (path=iiab_std_bin_dirs; *path; path++) { if (strncmp(iiab_dir_bin, *path, strlen(*path)) == 0) { stdplaces++; break; } } iiab_dir_launch = xnstrdup(cwd); if (stdplaces) { /*elog_printf(DIAG, "system installation detected: argv0 %s, %s", argv0, iiab_dir_bin);*/ iiab_dir_etc = xnstrdup(IIAB_STD_DIR_ETC); iiab_dir_lib = xnstrdup(IIAB_STD_DIR_LIB); iiab_dir_var = xnstrdup(IIAB_STD_DIR_VAR); iiab_dir_lock = xnstrdup(IIAB_STD_DIR_LOCK); } else { /* elog_printf(DIAG, "standalone installation detected: " "argv0 %s, %s", argv0, iiab_dir_bin);*/ iiab_dir_etc = util_strjoin(iiab_dir_bin, "/../etc", NULL); iiab_dir_lib = util_strjoin(iiab_dir_bin, "/../lib", NULL); iiab_dir_var = util_strjoin(iiab_dir_bin, "/../var", NULL); iiab_dir_lock = xnstrdup("/tmp"); /* check that the directories exist: if not, use the cwd */ if (access(iiab_dir_etc, R_OK)) { nfree(iiab_dir_etc); iiab_dir_etc = nstrdup(cwd); } if (access(iiab_dir_lib, R_OK)) { nfree(iiab_dir_lib); iiab_dir_lib = nstrdup(cwd); } if (access(iiab_dir_var, R_OK)) { nfree(iiab_dir_var); iiab_dir_var = nstrdup(cwd); } } }
CTag::CTag(const CTag& rTag) { m_uType = rTag.m_uType; m_uName = rTag.m_uName; m_pszName = rTag.m_pszName!=NULL ? nstrdup(rTag.m_pszName) : NULL; m_nBlobSize = 0; if (rTag.IsStr()) m_pstrVal = new CString(rTag.GetStr()); else if (rTag.IsInt()) m_uVal = rTag.GetInt(); else if (rTag.IsFloat()) m_fVal = rTag.GetFloat(); else if (rTag.IsHash()){ m_pData = new BYTE[16]; md4cpy(m_pData, rTag.GetHash()); } else if (rTag.IsBlob()){ m_nBlobSize = rTag.GetBlobSize(); m_pData = new BYTE[rTag.GetBlobSize()]; memcpy(m_pData, rTag.GetBlob(), rTag.GetBlobSize()); } else{ ASSERT(0); m_uVal = 0; } ASSERT_VALID(this); }
char * reverse (const char *host) { struct hostent hostinfo, *hostinfoptr; struct in_addr addr; int error; char *outhost; char buffer[BUFSIZE]; if (!host) { write_log (LOG_DEFAULT, "ERROR: reverse() called with NULL host"); return NULL; } xa_debug (1, "reverse() reverse resolving %s", host); if (inet_aton (host, &addr)) { hostinfoptr = ice_gethostbyaddr((char *) &addr, sizeof (struct in_addr), &hostinfo, buffer, BUFSIZE, &error); if (hostinfoptr && hostinfoptr->h_name) outhost = nstrdup (hostinfoptr->h_name); else outhost = NULL; ice_clean_hostent (); return outhost; } else return NULL; }
/** * write_header * Write the header value into the response map_header structure * return the size_t of the header written */ static size_t write_header(void * buffer, size_t size, size_t nitems, void * user_data) { struct _u_response * response = (struct _u_response *) user_data; char * header = (char *)buffer, * key, * value, * saveptr; if (strchr(header, ':') != NULL) { if (response->map_header != NULL) { // Expecting a header (key: value) key = trim_whitespace(strtok_r(header, ":", &saveptr)); value = trim_whitespace(strtok_r(NULL, ":", &saveptr)); u_map_put(response->map_header, key, value); } } else if (strlen(trim_whitespace(header)) > 0) { // Expecting the HTTP/x.x header if (response->protocol != NULL) { free(response->protocol); } response->protocol = nstrdup(header); if (response->protocol == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for response->protocol"); return 0; } } return nitems * size; }
/** * ulfius_send_http_request * Send a HTTP request and store the result into a _u_response * return U_OK on success */ int ulfius_send_http_request(const struct _u_request * request, struct _u_response * response) { body body_data; body_data.size = 0; body_data.data = NULL; int res; const char * content_type; res = ulfius_send_http_streaming_request(request, response, ulfius_write_body, (void *)&body_data); if (res == U_OK && response != NULL) { if (body_data.data != NULL && body_data.size > 0) { response->string_body = nstrdup(body_data.data); response->binary_body = malloc(body_data.size); if (response->binary_body == NULL || response->string_body == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for response->binary_body"); free(body_data.data); return U_ERROR_MEMORY; } memcpy(response->binary_body, body_data.data, body_data.size); response->binary_body_length = body_data.size; content_type = u_map_get_case(response->map_header, ULFIUS_HTTP_HEADER_CONTENT); if (content_type != NULL && 0 == nstrcmp(content_type, ULFIUS_HTTP_ENCODING_JSON)) { // Parsing json content response->json_body = json_loads(body_data.data, JSON_DECODE_ANY, NULL); } } free(body_data.data); return U_OK; } else { free(body_data.data); return res; } }
/** * ulfius_parse_url * fills map with the keys/values defined in the url that are described in the endpoint format url * return U_OK on success */ int ulfius_parse_url(const char * url, const struct _u_endpoint * endpoint, struct _u_map * map) { char * saveptr = NULL, * cur_word = NULL, * url_cpy = NULL, * url_cpy_addr = NULL; char * saveptr_format = NULL, * saveptr_prefix = NULL, * cur_word_format = NULL, * url_format_cpy = NULL, * url_format_cpy_addr = NULL; if (map != NULL && endpoint != NULL) { url_cpy = url_cpy_addr = nstrdup(url); url_format_cpy = url_format_cpy_addr = nstrdup(endpoint->url_prefix); cur_word = strtok_r( url_cpy, ULFIUS_URL_SEPARATOR, &saveptr ); if (endpoint->url_prefix != NULL && url_format_cpy == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for url_format_cpy"); } else if (url_format_cpy != NULL) { cur_word_format = strtok_r( url_format_cpy, ULFIUS_URL_SEPARATOR, &saveptr_prefix ); } while (cur_word_format != NULL && cur_word != NULL) { // Ignoring url_prefix words cur_word = strtok_r( NULL, ULFIUS_URL_SEPARATOR, &saveptr ); cur_word_format = strtok_r( NULL, ULFIUS_URL_SEPARATOR, &saveptr_prefix ); } free(url_format_cpy_addr); url_format_cpy = url_format_cpy_addr = nstrdup(endpoint->url_format); if (endpoint->url_format != NULL && url_format_cpy == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for url_format_cpy"); } else if (url_format_cpy != NULL) { cur_word_format = strtok_r( url_format_cpy, ULFIUS_URL_SEPARATOR, &saveptr_format ); } while (cur_word_format != NULL && cur_word != NULL) { if (cur_word_format[0] == ':' || cur_word_format[0] == '@') { if (u_map_put(map, cur_word_format+1, cur_word) != U_OK) { url_cpy_addr = NULL; url_format_cpy_addr = NULL; return U_ERROR_MEMORY; } } cur_word = strtok_r( NULL, ULFIUS_URL_SEPARATOR, &saveptr ); cur_word_format = strtok_r( NULL, ULFIUS_URL_SEPARATOR, &saveptr_format ); } free(url_cpy_addr); free(url_format_cpy_addr); url_cpy_addr = NULL; url_format_cpy_addr = NULL; return U_OK; } else { return U_ERROR_PARAMS; } }
CTag::CTag(LPCSTR pszName, const CString& rstrVal) { m_uType = TAGTYPE_STRING; m_uName = 0; m_pszName = nstrdup(pszName); m_pstrVal = new CString(rstrVal); m_nBlobSize = 0; ASSERT_VALID(this); }
CTag::CTag(LPCSTR pszName, uint32 uVal) { m_uType = TAGTYPE_UINT32; m_uName = 0; m_pszName = nstrdup(pszName); m_uVal = uVal; m_nBlobSize = 0; ASSERT_VALID(this); }
CTag::CTag(LPCSTR pszName, uint64 uVal, bool bInt64) { ASSERT( uVal <= 0xFFFFFFFF || bInt64 ); if (bInt64){ m_uType = TAGTYPE_UINT64; } else{ m_uType = TAGTYPE_UINT32; } m_uVal = uVal; m_uName = 0; m_pszName = nstrdup(pszName); m_nBlobSize = 0; ASSERT_VALID(this); }
void parse_cfgdict(text *in, Map ** m) { Map *mptr; char *ptr = VARDATA(in), *begin = NULL; char num = 0; int state = CS_WAITKEY; while (ptr - VARDATA(in) < VARSIZE(in) - VARHDRSZ) { if (*ptr == ',') num++; ptr++; } *m = mptr = (Map *) palloc(sizeof(Map) * (num + 2)); memset(mptr, 0, sizeof(Map) * (num + 2)); ptr = VARDATA(in); while (ptr - VARDATA(in) < VARSIZE(in) - VARHDRSZ) { if (state == CS_WAITKEY) { if (isalpha((unsigned char) *ptr)) { begin = ptr; state = CS_INKEY; } else if (!isspace((unsigned char) *ptr)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"), errdetail("Syntax error in position %d near \"%c\"", (int) (ptr - VARDATA(in)), *ptr))); } else if (state == CS_INKEY) { if (isspace((unsigned char) *ptr)) { mptr->key = nstrdup(begin, ptr - begin); state = CS_WAITEQ; } else if (*ptr == '=') { mptr->key = nstrdup(begin, ptr - begin); state = CS_WAITVALUE; } else if (!isalpha((unsigned char) *ptr)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"), errdetail("Syntax error in position %d near \"%c\"", (int) (ptr - VARDATA(in)), *ptr))); } else if (state == CS_WAITEQ) { if (*ptr == '=') state = CS_WAITVALUE; else if (!isspace((unsigned char) *ptr)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"), errdetail("Syntax error in position %d near \"%c\"", (int) (ptr - VARDATA(in)), *ptr))); } else if (state == CS_WAITVALUE) { if (*ptr == '"') { begin = ptr + 1; state = CS_INVALUE; } else if (!isspace((unsigned char) *ptr)) { begin = ptr; state = CS_IN2VALUE; } } else if (state == CS_INVALUE) { if (*ptr == '"') { mptr->value = nstrdup(begin, ptr - begin); mptr++; state = CS_WAITDELIM; } else if (*ptr == '\\') state = CS_INESC; } else if (state == CS_IN2VALUE) { if (isspace((unsigned char) *ptr) || *ptr == ',') { mptr->value = nstrdup(begin, ptr - begin); mptr++; state = (*ptr == ',') ? CS_WAITKEY : CS_WAITDELIM; } else if (*ptr == '\\') state = CS_INESC; } else if (state == CS_WAITDELIM) { if (*ptr == ',') state = CS_WAITKEY; else if (!isspace((unsigned char) *ptr)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("syntax error"), errdetail("Syntax error in position %d near \"%c\"", (int) (ptr - VARDATA(in)), *ptr))); } else if (state == CS_INESC) state = CS_INVALUE; else if (state == CS_IN2ESC) state = CS_IN2VALUE; else ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("bad parser state"), errdetail("%d at position %d near \"%c\"", state, (int) (ptr - VARDATA(in)), *ptr))); ptr++; } if (state == CS_IN2VALUE) { mptr->value = nstrdup(begin, ptr - begin); mptr++; } else if (!(state == CS_WAITDELIM || state == CS_WAITKEY)) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("unexpected end of line"))); }
/** * create a new request based on the source elements * returned value must be free'd */ struct _u_request * ulfius_duplicate_request(const struct _u_request * request) { struct _u_request * new_request = NULL; if (request != NULL) { new_request = malloc(sizeof(struct _u_request)); if (new_request == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for new_request"); return NULL; } if (ulfius_init_request(new_request) == U_OK) { new_request->http_verb = nstrdup(request->http_verb); new_request->http_url = nstrdup(request->http_url); if (new_request->http_verb == NULL || new_request->http_url == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for ulfius_duplicate_request"); ulfius_clean_request_full(new_request); return NULL; } if (request->client_address != NULL) { new_request->client_address = malloc(sizeof(struct sockaddr)); if (new_request->client_address == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for new_request->client_address"); ulfius_clean_request_full(new_request); return NULL; } memcpy(new_request->client_address, request->client_address, sizeof(struct sockaddr)); } u_map_clean_full(new_request->map_url); u_map_clean_full(new_request->map_header); u_map_clean_full(new_request->map_cookie); u_map_clean_full(new_request->map_post_body); new_request->map_url = u_map_copy(request->map_url); new_request->map_header = u_map_copy(request->map_header); new_request->map_cookie = u_map_copy(request->map_cookie); new_request->map_post_body = u_map_copy(request->map_post_body); new_request->json_body = json_copy(request->json_body); new_request->json_has_error = request->json_has_error; if ((new_request->map_url == NULL && request->map_url != NULL) || (new_request->map_header == NULL && request->map_header != NULL) || (new_request->map_cookie == NULL && request->map_cookie != NULL) || (new_request->map_post_body == NULL && request->map_post_body != NULL) || (new_request->json_body == NULL && request->json_body != NULL)) { ulfius_clean_request_full(new_request); return NULL; } if (request->binary_body != NULL && request->binary_body_length > 0) { new_request->binary_body = malloc(request->binary_body_length); if (new_request->binary_body == NULL) { y_log_message(Y_LOG_LEVEL_ERROR, "Ulfius - Error allocating memory for new_request->binary_body"); ulfius_clean_request_full(new_request); return NULL; } memcpy(new_request->binary_body, request->binary_body, request->binary_body_length); } else { new_request->binary_body_length = 0; new_request->binary_body = NULL; } new_request->binary_body_length = request->binary_body_length; } else { free(new_request); new_request = NULL; } } return new_request; }