[HTTP] add HTTP AT function
Change-Id: I467ca29cb128c0d5e7c7291bde20c4290d45b93f
diff --git a/mbtk/mbtk_lib/src/mbtk_http.c b/mbtk/mbtk_lib/src/mbtk_http.c
index e63064e..b4e58ee 100755
--- a/mbtk/mbtk_lib/src/mbtk_http.c
+++ b/mbtk/mbtk_lib/src/mbtk_http.c
@@ -10,6 +10,9 @@
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
#include "mbtk_http_base.h"
#include "mbtk_http.h"
@@ -18,6 +21,9 @@
/*************************************************************
Constants and Macros
*************************************************************/
+#define BUFFER_SIZE 1024
+
+
/*************************************************************
Variables:local
@@ -493,6 +499,7 @@
char line[BUFFER_SIZE];
char *ptr = NULL;
int len = 0;
+ int write_len = 0;
while((len = mbtk_http_read_line(session->sock_fd,line,BUFFER_SIZE)) > 0)
{
if(!memcmp(line,"\r\n",2))
@@ -500,7 +507,7 @@
LOGD("Read empty line.");
break;
}
-
+
// Delete "\r\n"
ptr = line + len - 1; // Point to last char.
while(http_is_space_char(*ptr))
@@ -511,13 +518,29 @@
}
LOGV("LINE:%s",line);
-
- if(http_handles[session->handle_id].show_rsp_header &&
- http_handles[session->handle_id].data_cb)
+ if(session->type)
{
- http_handles[session->handle_id].data_cb(session->id,
- MBTK_HTTP_DATA_HEADER,line,len);
+ /*
+ if(session->file_fd >= 0)
+ {
+ write_len = write(session->file_fd, line, len);
+ if(write_len < len) {
+ LOGE("write() fail.");
+ return -1;
+ }
+ }
+ */
}
+ else
+ {
+ if(http_handles[session->handle_id].show_rsp_header &&
+ http_handles[session->handle_id].data_cb)
+ {
+ http_handles[session->handle_id].data_cb(session->id,
+ MBTK_HTTP_DATA_HEADER,line,len);
+ }
+
+ }
if(!memcmp(line,"HTTP/",5)) // "HTTP/1.1 200 OK"
{
@@ -652,7 +675,7 @@
char read_buf[BUFFER_SIZE + 1];
int read_len = 0;
char chunk_buf[BUFFER_SIZE + 1];
- int chunk_len;
+ int chunk_len = 0, write_len=0;
http_chunk_init(&chunker);
while(TRUE)
{
@@ -674,9 +697,24 @@
LOGD("Read chunk_len:%d",chunk_len);
chunk_buf[chunk_len] = '\0';
- if(http_handles[session->handle_id].data_cb)
- http_handles[session->handle_id].data_cb(session->id,
- MBTK_HTTP_DATA_CONTENT,chunk_buf,chunk_len);
+ if(session->type)
+ {
+ if(session->file_fd >= 0)
+ {
+ write_len = write(session->file_fd, chunk_buf, chunk_len);
+ if(write_len < chunk_len) {
+ LOGE("write() fail.");
+ return -1;
+ }
+ }
+ }
+ else
+ {
+ if(http_handles[session->handle_id].data_cb)
+ http_handles[session->handle_id].data_cb(session->id,
+ MBTK_HTTP_DATA_CONTENT,chunk_buf,chunk_len);
+ usleep(100000);
+ }
if(CHUNKE_STOP == chunk_code)
{
@@ -699,6 +737,7 @@
#define BUFFER_SIZE 2048
char read_buf[BUFFER_SIZE + 1];
int read_len = 0;
+ int write_len = 0;
int64 read_count = 0;
while(TRUE)
{
@@ -710,24 +749,52 @@
return -1;
}
+
if(read_count + read_len >= session->rsp.content_length) // Read data complete.
{
+ if(session->file_fd >= 0)
+ {
+ write_len = write(session->file_fd, read_buf, read_len);
+ if(write_len < read_len) {
+ LOGE("write() fail.");
+ return -1;
+ }
+ }
+
if(http_handles[session->handle_id].data_cb)
{
http_handles[session->handle_id].data_cb(session->id,
MBTK_HTTP_DATA_CONTENT,read_buf,session->rsp.content_length - read_count);
-
+
http_handles[session->handle_id].data_cb(session->id,
MBTK_HTTP_DATA_COMPLETE,NULL,0);
}
break;
}
- if(http_handles[session->handle_id].data_cb)
- http_handles[session->handle_id].data_cb(session->id,
- MBTK_HTTP_DATA_CONTENT,read_buf,read_len);
+ LOGE("read_len :%d, session->type:%d\n", read_len, session->type);
+ if(session->type)
+ {
+ if(session->file_fd >= 0)
+ {
+ write_len = write(session->file_fd, read_buf, read_len);
+ if(write_len < read_len) {
+ LOGE("write() fail.");
+ return -1;
+ }
+ }
+ }
+ else
+ {
+ if(http_handles[session->handle_id].data_cb)
+ http_handles[session->handle_id].data_cb(session->id,
+ MBTK_HTTP_DATA_CONTENT,read_buf,read_len);
+ usleep(100000);
+ }
read_count += read_len;
+ LOGE("mbtk1111111111111111111111113by read_count:%d, read_len:%d\n.", read_count, read_len);
+
}
return 0;
@@ -739,6 +806,7 @@
#define BUFFER_SIZE 2048
char read_buf[BUFFER_SIZE + 1];
int read_len = 0;
+ int write_len = 0;
while(TRUE)
{
read_len = mbtk_http_read(session->sock_fd,read_buf,BUFFER_SIZE,1000);
@@ -751,18 +819,37 @@
//return -1;
break;
}
-
+
read_buf[read_len] = '\0';
- if(http_handles[session->handle_id].data_cb)
- http_handles[session->handle_id].data_cb(session->id,
- MBTK_HTTP_DATA_CONTENT,read_buf,read_len);
+ if(session->type)
+ {
+ if(session->file_fd >= 0)
+ {
+ write_len = write(session->file_fd, read_buf, read_len);
+ if(write_len < read_len) {
+ LOGE("write() fail.");
+ return -1;
+ }
+ }
+ }
+ else
+ {
+ if(http_handles[session->handle_id].data_cb)
+ http_handles[session->handle_id].data_cb(session->id,
+ MBTK_HTTP_DATA_CONTENT,read_buf,read_len);
+
+ usleep(100000);
+ }
+ LOGE("mbtk1111111111111111111111111by general\n.");
+
}
if(http_handles[session->handle_id].data_cb)
http_handles[session->handle_id].data_cb(session->id,
MBTK_HTTP_DATA_COMPLETE,NULL,0);
+
return 0;
}
@@ -770,7 +857,18 @@
{
LOGI("Start HTTP read.");
int result = 0;
+
// usleep(500000);
+ if(session->type)
+ {
+ session->file_fd = open("/http_test.txt", O_WRONLY|O_TRUNC|O_CREAT|O_APPEND, 0666);
+
+ if(session->file_fd < 0) {
+ LOGE("open() fail.");
+ return -1;
+ }
+ }
+
session->state = HTTP_SESSION_STATE_READ_HEADER;
if(http_header_read(session))
{
@@ -785,6 +883,7 @@
{
if(http_session_read_by_chunk(session))
{
+ LOGE("http_session_read_by_chunk fail.");
result = -1;
goto read_end;
}
@@ -793,6 +892,7 @@
{
if(http_session_read_by_length(session))
{
+ LOGE("http_session_read_by_length fail.");
result = -1;
goto read_end;
}
@@ -801,6 +901,7 @@
{
if(http_session_read_by_general(session))
{
+ LOGE("http_session_read_by_general fail.");
result = -1;
goto read_end;
}
@@ -816,6 +917,18 @@
read_end:
session->state = HTTP_SESSION_STATE_READ_END;
+ if(session->file_fd >= 0)
+ {
+ if(close(session->file_fd))
+ {
+ LOGI("close() fail file_fd:%d",session->file_fd);
+ session->file_fd = -1;
+ return -1;
+ }
+ session->file_fd = -1;
+ }
+
+
LOGI("HTTP request complete[result - %d].",result);
if(http_session_close(session))
{
@@ -1019,7 +1132,7 @@
}
memset(session,0x0,sizeof(mbtk_http_session_t));
session->sock_fd = -1;
- session->sock_file = NULL;
+ session->file_fd = -1;
session->handle_id = handle_id;
session->id = session_index;
session->state = HTTP_SESSION_STATE_NON;
@@ -1056,6 +1169,26 @@
return 0;
}
+int mbtk_http_session_type_set(int handle_id, int session_id, mbtk_http_type_enum type)
+{
+ if(!http_session_check(handle_id,session_id))
+ {
+ LOGE("Session error.");
+ return -1;
+ }
+
+ mbtk_http_session_t *session = http_handles[handle_id].session[session_id];
+ if(session->state != HTTP_SESSION_STATE_NON)
+ {
+ LOGE("Session state error.[%d]",session->state);
+ return -1;
+ }
+
+ session->type = type;
+ LOGE("session->type:%d, type:%d\n", session->type, type);
+ return 0;
+}
+
int mbtk_http_session_free(int handle_id,int session_id)
{
if(!http_session_check(handle_id,session_id))
@@ -1359,3 +1492,92 @@
return http_handles[handle_id].session[session_id];
}
+
+int mbtk_http_get_file_size(char *file_name)
+{
+ int ret;
+ int fd = -1;
+ struct stat file_stat;
+
+ LOGE("mbtk_http_get_file_size() start\n");
+
+ fd = open(file_name, O_RDONLY); // ??¡§¡ã?a???t
+ if (fd == -1) {
+ printf("Open file %s failed\n", file_name);
+ return -1;
+ }
+ ret = fstat(fd, &file_stat); // ??¡§¡§????t?¨¢??¡§??
+ if (ret == -1) {
+ printf("Get file %s stat failed\n", file_name);
+ close(fd);
+ return -1;
+ }
+ close(fd);
+ LOGE("mbtk_http_get_file_size() end\n");
+ return file_stat.st_size;
+}
+
+
+int mbtk_http_read_file_data(int handle_id, int session_id,
+ mbtk_http_data_file_type_enum type,int offset, mbtk_http_data_file_callback_func data_file_cb)
+{
+
+ if(!http_session_check(handle_id,session_id))
+ {
+ LOGE("Session error.");
+ return -1;
+ }
+
+ mbtk_http_session_t *session = http_handles[handle_id].session[session_id];
+ if(session != NULL)
+ {
+ http_handles[session->handle_id].data_file_cb = data_file_cb;
+
+ if(type) //1 read file size
+ {
+ int size = mbtk_http_get_file_size("/http_test.txt");
+ http_handles[session->handle_id].data_file_cb(type, NULL, size);
+ }else //read file data
+ {
+ char buf[1024+1] = {0};
+ int ret = 0;
+
+ int fd = open("/http_test.txt", O_RDONLY);
+ if (fd == -1) {
+ printf("Open file %s failed\n", "/http_test.txt");
+ return -1;
+ }
+
+ if(offset)
+ {
+ lseek(fd, offset, SEEK_SET);
+ }
+// usleep(500000);
+ int index = 0;
+ while(1)
+ {
+ memset(buf,0, sizeof(buf));
+ ret = read(fd, buf, 1024);
+ printf("ret = %d , ", ret);
+ usleep(100000);
+ LOGE("mbtk, read file:%s\n", buf);
+ if(ret <= 0)
+ break;
+
+ http_handles[session->handle_id].data_file_cb(type, buf, ret);
+ index++;
+ }
+ close(fd);
+ printf("%d\n", index);
+
+ LOGE("read()end index;:%d, ret:%d",index,ret);
+ }
+ }
+
+
+ return 0;
+}
+
+
+
+