Skip to content

Commit

Permalink
✨ add use SqlConnRAII
Browse files Browse the repository at this point in the history
  • Loading branch information
markparticle committed Jun 28, 2020
1 parent 99bb53e commit 3cb745b
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 130 deletions.
6 changes: 2 additions & 4 deletions code/http/httpconn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ const char* HttpConn::srcDir;
std::atomic<int> HttpConn::userCount;
bool HttpConn::isET;


HttpConn::HttpConn() {
fd_ = -1;
addr_ = { 0 };
Expand Down Expand Up @@ -103,7 +102,7 @@ ssize_t HttpConn::write(int* saveErrno) {
response_.UnmapFile();
break;
}
} while(isET || ToWriteBytes() > 10240);
} while(isET || ToWriteBytes() > 10240); /* LT模式的大文件传输用while */
return len;
}

Expand All @@ -119,10 +118,9 @@ void HttpConn::process() {

iov_[0].iov_base = writeBuff_.Peek();
iov_[0].iov_len = writeBuff_.ReadableBytes();
// LOG_DEBUG("\n%s\n", writeBuff_.Peek());
iovCnt_ = 1;

if(response_.FileLen() > 0 && response_.File()) {
//LOG_DEBUG("\n%s\n", response_.File());
iov_[1].iov_base = response_.File();
iov_[1].iov_len = response_.FileLen();
iovCnt_ = 2;
Expand Down
18 changes: 4 additions & 14 deletions code/http/httpconn.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,11 @@
#ifndef HTTP_CONN_H
#define HTTP_CONN_H

#include <sys/socket.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h> // stat()
#include <sys/uio.h> // readv/writev

#include <arpa/inet.h> // sockaddr_in

#include <stdio.h> // vsnparintf()
#include <stdlib.h> //atoi()
#include <string.h> //strcpy()
#include <stdarg.h> // va_list


#include <errno.h> // errno
#include <sys/uio.h> // readv/writev
#include <arpa/inet.h> // sockaddr_in
#include <stdlib.h> // atoi()
#include <errno.h>

#include "../log/log.h"
#include "../pool/sqlconnRAII.h"
Expand Down
58 changes: 52 additions & 6 deletions code/http/httprequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ const unordered_set<string> HttpRequest::DEFAULT_HTML{
const unordered_map<string, int> HttpRequest::DEFAULT_HTML_TAG {
{"/register.html", 0}, {"/login.html", 1}, };

void HttpRequest::Init() {
method_ = path_ = version_ = body_ = "";
state_ = REQUEST_LINE;
header_.clear();
post_.clear();
}

bool HttpRequest::IsKeepAlive() const {
if(post_.count("Connection") == 1) {
return post_.find("Connection")->second == "Keep-Alive" && version_ == "1.1";
}
return false;
}

bool HttpRequest::parse(Buffer& buff) {
const char* CRLF = "\r\n";
if(buff.ReadableBytes() <= 0) {
Expand Down Expand Up @@ -42,7 +56,7 @@ bool HttpRequest::parse(Buffer& buff) {
default:
break;
}
if(lineEnd == buff.BeginWrite()) {break;}
if(lineEnd == buff.BeginWrite()) { break; }
buff.RetrieveUntil(lineEnd + 2);

}
Expand All @@ -53,7 +67,7 @@ bool HttpRequest::parse(Buffer& buff) {
void HttpRequest::ParsePath_() {
if(path_ == "/") {
path_ = "/index.html";
}
}
else {
for(auto &item: DEFAULT_HTML) {
if(item == path_) {
Expand Down Expand Up @@ -167,8 +181,9 @@ void HttpRequest::ParseFromUrlencoded_() {
bool HttpRequest::UserVerify(const string &name, const string &pwd, bool isLogin) {
if(name == "" || pwd == "") { return false; }
LOG_INFO("Verify name:%s pwd:%s", name.c_str(), pwd.c_str());

MYSQL* sql = SqlConnPool::Instance()->GetConn();
MYSQL* sql;
SqlConnRAII(&sql, SqlConnPool::Instance());

bool flag = false;
unsigned int j = 0;
char order[256] = { 0 };
Expand All @@ -177,7 +192,7 @@ bool HttpRequest::UserVerify(const string &name, const string &pwd, bool isLogin

if(!isLogin) { flag = true; }
/* 查询用户及密码 */
sprintf(order, "SELECT username, password FROM user WHERE username='%s' LIMIT 1", name.c_str());
snprintf(order, 128, "SELECT username, password FROM user WHERE username='%s' LIMIT 1", name.c_str());
LOG_DEBUG("%s", order);

if(mysql_query(sql, order)) {
Expand Down Expand Up @@ -209,7 +224,7 @@ bool HttpRequest::UserVerify(const string &name, const string &pwd, bool isLogin
/* 注册行为 且 用户名未被使用*/
if(!isLogin && flag == true) {
LOG_DEBUG("regirster!");
sprintf(order, "INSERT INTO user(username, password) VALUES('%s','%s')", name.c_str(), pwd.c_str());
snprintf(order, 128,"INSERT INTO user(username, password) VALUES('%s','%s')", name.c_str(), pwd.c_str());
LOG_DEBUG( "%s", order);
if(mysql_query(sql, order)) {
LOG_DEBUG( "Insert error!");
Expand All @@ -220,4 +235,35 @@ bool HttpRequest::UserVerify(const string &name, const string &pwd, bool isLogin
SqlConnPool::Instance()->FreeConn(sql);
LOG_DEBUG( "UserVerify success!!");
return flag;
}

std::string HttpRequest::path() const{
return path_;
}

std::string& HttpRequest::path(){
return path_;
}
std::string HttpRequest::method() const {
return method_;
}

std::string HttpRequest::version() const {
return version_;
}

std::string HttpRequest::GetPost(const std::string& key) const {
assert(key != "");
if(post_.count(key) == 1) {
return post_.find(key)->second;
}
return "";
}

std::string HttpRequest::GetPost(const char* key) const {
assert(key != nullptr);
if(post_.count(key) == 1) {
return post_.find(key)->second;
}
return "";
}
55 changes: 14 additions & 41 deletions code/http/httprequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@
#include <unordered_set>
#include <string>
#include <regex>
#include <errno.h> // errno
#include <mysql/mysql.h> //mysql
#include <errno.h>
#include <mysql/mysql.h> //mysql

#include "../buffer/buffer.h"
#include "../log/log.h"
#include "../pool/sqlconnpool.h"
#include "../pool/sqlconnRAII.h"

class HttpRequest {
public:
HttpRequest() {
Init();
}

enum PARSE_STATE {
REQUEST_LINE,
HEADERS,
Expand All @@ -41,44 +38,20 @@ class HttpRequest {
CLOSED_CONNECTION,
};

bool parse(Buffer& buff);

std::string path() const{
return path_;
}

std::string& path(){
return path_;
}
std::string method() const {
return method_;
}

std::string version() const {
return method_;
}
HttpRequest() { Init(); }
~HttpRequest() = default;

std::string GetPost(const std::string& key) const {
assert(key != "");
if(post_.count(key) == 1) {
return post_.find(key)->second;
}
return "";
}
void Init();
bool parse(Buffer& buff);

bool IsKeepAlive() const {
if(post_.count("Connection") == 1) {
return post_.find("Connection")->second == "Keep-Alive" && version_ == "1.1";
}
return false;
}
std::string path() const;
std::string& path();
std::string method() const;
std::string version() const;
std::string GetPost(const std::string& key) const;
std::string GetPost(const char* key) const;

void Init() {
method_ = path_ = version_ = body_ = "";
state_ = REQUEST_LINE;
header_.clear();
post_.clear();
}
bool IsKeepAlive() const;

/*
todo
Expand Down
Loading

0 comments on commit 3cb745b

Please sign in to comment.