Ejemplo n.º 1
0
void belle_sip_multipart_body_handler_progress_cb(belle_sip_body_handler_t *obj, belle_sip_message_t *msg, void *user_data, size_t transfered, size_t expected_total) {
	if (transfered == expected_total) {
		/* The full multipart body has been received, we can now parse it and split the different parts,
		 * creating a belle_sip_memory_body_handler for each part and adding them to the belle_sip_multipart_body_handler
		 * parts list. */
		belle_sip_multipart_body_handler_t *obj_multipart = (belle_sip_multipart_body_handler_t *)obj;
		belle_sip_memory_body_handler_t *memorypart;
		belle_sip_header_t *header;
		uint8_t *end_part_cursor;
		uint8_t *end_headers_cursor;
		uint8_t *end_header_cursor;
		uint8_t *cursor = obj_multipart->buffer;
		char *boundary = belle_sip_strdup_printf("--%s", obj_multipart->boundary);
		if (strncmp((char *)cursor, boundary, strlen(boundary))) {
			belle_sip_warning("belle_sip_multipart_body_handler [%p]: body not starting by specified boundary '%s'", obj_multipart, obj_multipart->boundary);
			belle_sip_free(boundary);
			return;
		}
		cursor += strlen(boundary);
		do {
			if (strncmp((char *)cursor, "\r\n", 2)) {
				belle_sip_warning("belle_sip_multipart_body_handler [%p]: no new-line after boundary", obj_multipart);
				return;
			}
			cursor += 2;
			end_part_cursor = (uint8_t *)strstr((char *)cursor, boundary);
			if (end_part_cursor == NULL) {
				belle_sip_warning("belle_sip_multipart_body_handler [%p]: cannot find next boundary", obj_multipart);
				return;
			} else {
				*end_part_cursor = 0;
				end_headers_cursor = (uint8_t *)strstr((char *)cursor, "\r\n\r\n");
				if (end_headers_cursor == NULL) {
					memorypart = belle_sip_memory_body_handler_new_copy_from_buffer(cursor, strlen((char *)cursor), NULL, NULL);
				} else {
					uint8_t *begin_body_cursor = end_headers_cursor + 4;
					memorypart = belle_sip_memory_body_handler_new_copy_from_buffer(begin_body_cursor, strlen((char *)begin_body_cursor), NULL, NULL);
					do {
						end_header_cursor = (uint8_t *)strstr((char *)cursor, "\r\n");
						*end_header_cursor = 0;
						header = belle_sip_header_parse((char *)cursor);
						if (header != NULL) {
							belle_sip_body_handler_add_header(BELLE_SIP_BODY_HANDLER(memorypart), header);
						}
						cursor = end_header_cursor + 2;
					} while (end_header_cursor != end_headers_cursor);
				}
				belle_sip_multipart_body_handler_add_part(obj_multipart, BELLE_SIP_BODY_HANDLER(memorypart));
				cursor = end_part_cursor + strlen(boundary);
			}
		} while (strcmp((char *)cursor, "--\r\n"));
		belle_sip_free(boundary);
	}
}
Ejemplo n.º 2
0
SalCustomHeader *sal_custom_header_append(SalCustomHeader *ch, const char *name, const char *value){
	belle_sip_message_t *msg=(belle_sip_message_t*)ch;
	belle_sip_header_t *h;
	char *tmp=ms_strdup_printf("%s: %s\r\n",name,value);
	
	if (msg==NULL){
		msg=(belle_sip_message_t*)belle_sip_request_new();
		belle_sip_object_ref(msg);
	}
	h=belle_sip_header_parse(tmp);
	ms_free(tmp);
	if (h==NULL){
		belle_sip_error("Fail to parse extension header.");
		return (SalCustomHeader*)msg;
	}
	belle_sip_message_add_header(msg,h);
	return (SalCustomHeader*)msg;
}