Skip to content

Commit

Permalink
fix mg_read bug: can recieve any length input
Browse files Browse the repository at this point in the history
  • Loading branch information
carfly committed Jul 22, 2013
1 parent 5b3b8ff commit 8a087cd
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/server/ltp_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,33 +60,28 @@ int main(int argc, char *argv[])

static int Service(struct mg_connection *conn)
{
char sentence[POST_LEN];
char type[POST_LEN];
char xml[POST_LEN];
char post_data[POST_LEN];
int post_data_len;
char *sentence;
char type[10];
char xml[10];
char buffer[POST_LEN];

string str_post_data;
string str_type;
string str_xml;

const struct mg_request_info *ri = mg_get_request_info(conn);

if (!strcmp(ri->uri, "/ltp")) {
post_data_len = mg_read(conn, post_data, sizeof(post_data));
cout << post_data_len << " " << post_data << endl;

/*const char *qs = ri->query_string;
cout << "query_string: " << qs << endl;
mg_get_var(qs, strlen(qs == NULL ? "" : qs), "s", sentence, sizeof(sentence));
cout << "sentence: " << sentence << endl;
int len;
while((len = mg_read(conn, buffer, sizeof(buffer) - 1)) > 0){
str_post_data += buffer;
}
cout << str_post_data.size() << " " << str_post_data << endl;

mg_get_var(qs, strlen(qs == NULL ? "" : qs), "t", type, sizeof(type));
mg_get_var(qs, strlen(qs == NULL ? "" : qs), "x", xml, sizeof(xml));
*/
mg_get_var(post_data, post_data_len, "s", sentence, sizeof(sentence));
mg_get_var(post_data, post_data_len, "t", type, sizeof(type));
mg_get_var(post_data, post_data_len, "x", xml, sizeof(xml));
sentence = new char[str_post_data.size() + 1];
mg_get_var(str_post_data.c_str(), str_post_data.size(), "s", sentence, str_post_data.size());
mg_get_var(str_post_data.c_str(), str_post_data.size(), "t", type, sizeof(type) - 1);
mg_get_var(str_post_data.c_str(), str_post_data.size(), "x", xml, sizeof(xml) - 1);

if (strcmp(sentence, "") == 0)
return 0;
Expand All @@ -104,6 +99,7 @@ static int Service(struct mg_connection *conn)
}

string strSentence = sentence;
delete []sentence;

cout << "Input sentence is: " << strSentence << endl;

Expand Down

0 comments on commit 8a087cd

Please sign in to comment.