/*---------------------------------------------------------------------- | ATX_HttpUrl_Destruct +---------------------------------------------------------------------*/ static ATX_Result ATX_HttpUrl_Destruct(ATX_HttpUrl* self) { ATX_String_Destruct(&self->username); ATX_String_Destruct(&self->password); ATX_String_Destruct(&self->host); ATX_String_Destruct(&self->path); return ATX_SUCCESS; }
/*---------------------------------------------------------------------- | ATX_HttpHeader_Destroy +---------------------------------------------------------------------*/ static ATX_Result ATX_HttpHeader_Destroy(ATX_HttpHeader* header) { /* free the strings */ ATX_String_Destruct(&header->name); ATX_String_Destruct(&header->value); /* free the object */ ATX_FreeMemory(header); return ATX_SUCCESS; }
/*---------------------------------------------------------------------- | Listener_Destroy +---------------------------------------------------------------------*/ ATX_METHOD Listener_Destroy(ATX_Destroyable* _self) { Listener* self = ATX_SELF(Listener, ATX_Destroyable); ATX_String_Destruct(&self->name); ATX_FreeMemory((void*)self); return ATX_SUCCESS; }
/*---------------------------------------------------------------------- | ATX_HttpResponse_Destroy +---------------------------------------------------------------------*/ ATX_Result ATX_HttpResponse_Destroy(ATX_HttpResponse* response) { /* destruct the base object */ ATX_HttpMessage_Destruct(&response->base); /* destruct the object */ ATX_String_Destruct(&response->reason_phrase); /* free the object */ ATX_FreeMemory(response); return ATX_SUCCESS; }
/*---------------------------------------------------------------------- | BLT_TcpNetworkStream_Create +---------------------------------------------------------------------*/ BLT_Result BLT_TcpNetworkStream_Create(const char* name, ATX_InputStream** stream) { ATX_Socket* sock; ATX_String hostname = ATX_String_Create(name); ATX_UInt16 port = BLT_TCP_NETWORK_STREAM_DEFAULT_PORT; int sep; ATX_Result result = ATX_SUCCESS; /* default */ *stream = NULL; /* parse the hostname/port */ sep = ATX_String_FindCharFrom(&hostname, ':', 6); if (sep > 0) { /* we have a port number */ int port_long = 0; result = ATX_ParseInteger(name+sep+1, &port_long, ATX_FALSE); if (ATX_FAILED(result)) { ATX_LOG_WARNING("BLT_TcpNetworkStream_Create - invalid port spec"); goto end; } port = (ATX_UInt16)port_long; ATX_String_SetLength(&hostname, sep); } /* create a socket */ result = ATX_TcpClientSocket_Create(&sock); if (ATX_FAILED(result)) goto end; /* connect */ ATX_LOG_FINE_2("BLT_TcpNetworkStream_Create - connecting to %s:%d", ATX_CSTR(hostname), port); result = ATX_Socket_ConnectToHost(sock, ATX_CSTR(hostname), port, BLT_TCP_NETWORK_STREAM_DEFAULT_TIMEOUT); if (ATX_FAILED(result)) { ATX_LOG_WARNING_1("BLT_TcpNetworkStream_Create - failed to connect (%d)", result); goto end; } ATX_LOG_FINE("BLT_TcpNetworkStream_Create - connected"); /* return the input stream */ result = ATX_Socket_GetInputStream(sock, stream); /* release the socket */ ATX_DESTROY_OBJECT(sock); end: ATX_String_Destruct(&hostname); return result; }
/*---------------------------------------------------------------------- | StdcFile_Destroy +---------------------------------------------------------------------*/ ATX_METHOD StdcFile_Destroy(ATX_Destroyable* _self) { StdcFile* self = ATX_SELF(StdcFile, ATX_Destroyable); /* release the resources */ ATX_String_Destruct(&self->name); StdcFileWrapper_Release(self->file); /* free the memory */ ATX_FreeMemory((void*)self); return ATX_SUCCESS; }
/*---------------------------------------------------------------------- | ATX_HttpRequest_Destroy +---------------------------------------------------------------------*/ ATX_Result ATX_HttpRequest_Destroy(ATX_HttpRequest* request) { /* destruct the base object */ ATX_HttpMessage_Destruct(&request->base); /* destruct the object */ ATX_String_Destruct(&request->method); ATX_HttpUrl_Destruct(&request->url); /* free the object */ ATX_FreeMemory(request); return ATX_SUCCESS; }
/*---------------------------------------------------------------------- | ATX_HttpMessage_Destruct +---------------------------------------------------------------------*/ static ATX_Result ATX_HttpMessage_Destruct(ATX_HttpMessage* message) { /* destroy all headers */ ATX_ListItem* item = ATX_List_GetFirstItem(message->headers); while (item) { ATX_HttpHeader_Destroy((ATX_HttpHeader*)ATX_ListItem_GetData(item)); item = ATX_ListItem_GetNext(item); } ATX_List_Destroy(message->headers); /* free the protocol string */ ATX_String_Destruct(&message->protocol); /* release the body stream */ ATX_RELEASE_OBJECT(message->body); return ATX_SUCCESS; }
/*---------------------------------------------------------------------- | AlsaOutput_Destroy +---------------------------------------------------------------------*/ static BLT_Result AlsaOutput_Destroy(AlsaOutput* self) { ATX_LOG_FINE("destroying output"); /* close the device */ AlsaOutput_Close(self); /* free the name */ ATX_String_Destruct(&self->device_name); /* destruct the inherited object */ BLT_BaseMediaNode_Destruct(&ATX_BASE(self, BLT_BaseMediaNode)); /* free the object memory */ ATX_FreeMemory(self); return BLT_SUCCESS; }
/*---------------------------------------------------------------------- | ATX_HttpResponse_Parse +---------------------------------------------------------------------*/ static ATX_Result ATX_HttpResponse_Parse(ATX_HttpResponse* response, ATX_InputStream* stream) { char buffer[ATX_HTTP_MAX_LINE_SIZE+1]; char* line = buffer; char* find; ATX_Boolean header_pending = ATX_FALSE; ATX_String header_name = ATX_EMPTY_STRING; ATX_String header_value = ATX_EMPTY_STRING; ATX_Result result; /* get the first line from the stream */ result = ATX_InputStream_ReadLine(stream, line, sizeof(buffer), NULL); if (ATX_FAILED(result)) return result; /* get the protocol */ find = (char*)ATX_Http_FindChar(line, ' '); if (find == NULL) { return ATX_ERROR_INVALID_SYNTAX; } *find = '\0'; ATX_String_Assign(&response->base.protocol, line); /* get the status code */ line = (char*)ATX_Http_SkipWhitespace(find+1); find = (char*)ATX_Http_FindChar(line, ' '); if (find == NULL) { return ATX_ERROR_INVALID_SYNTAX; } *find = '\0'; if (ATX_StringLength(line) != 3) { return ATX_ERROR_INVALID_SYNTAX; } { int i; response->status_code = 0; for (i=0; i<3; i++) { if (line[i] < '0' || line[i] > '9') { return ATX_ERROR_INVALID_SYNTAX; } response->status_code *= 10; response->status_code += line[i]-'0'; } } /* the rest is the reason phrase */ line = (char*)ATX_Http_SkipWhitespace(find+1); ATX_String_Assign(&response->reason_phrase, line); /* parse headers until an empty line or end of stream */ do { /* read a line */ result = ATX_InputStream_ReadLine(stream, line, sizeof(buffer), NULL); if (ATX_FAILED(result)) break; /* stop if line is empty */ if (line[0] == '\0' || line[0] == '\r' || line[0] == '\n') { if (header_pending) { ATX_String_TrimWhitespace(&header_value); ATX_HttpMessage_SetHeader((ATX_HttpMessage*)response, ATX_CSTR(header_name), ATX_CSTR(header_value)); ATX_LOG_FINE_2("ATX_HttpResponse::Parse - %s: %s", ATX_CSTR(header_name), ATX_CSTR(header_value)); } break; } /* process the line */ if ((line[0] == ' ' || line[0] == '\t') && header_pending) { /* this is a line continuation */ ATX_String_Append(&header_value, line+1); } else { /* this is a new header */ const char* name; const char* value; /* add the pending header to the list */ if (header_pending) { ATX_String_TrimWhitespace(&header_value); ATX_HttpMessage_SetHeader((ATX_HttpMessage*)response, ATX_CSTR(header_name), ATX_CSTR(header_value)); ATX_LOG_FINE_2("ATX_HttpResponse::Parse - %s: %s", ATX_CSTR(header_name), ATX_CSTR(header_value)); } /* parse header name */ name = ATX_Http_SkipWhitespace(line); value = ATX_Http_FindChar(name, ':'); ATX_String_AssignN(&header_name, name, (ATX_Size)(value-name)); value = ATX_Http_SkipWhitespace(value+1); ATX_String_Assign(&header_value, value); /* don't add the header now, it could be continued */ header_pending = ATX_TRUE; } } while(ATX_SUCCEEDED(result)); /* keep a reference to the stream */ response->base.body = stream; ATX_REFERENCE_OBJECT(stream); /* cleanup */ ATX_String_Destruct(&header_name); ATX_String_Destruct(&header_value); return ATX_SUCCESS; }