~jan-kneschke/mysql-proxy/packet-tracking-assertions

« back to all changes in this revision

Viewing changes to tags/mysql-proxy-0.6.1/examples/tutorial-inject.lua

  • Committer: Kay Roepke
  • Author(s): Jan Kneschke
  • Date: 2008-01-23 22:00:28 UTC
  • Revision ID: kay@mysql.com-20080123220028-hq2xqb69apa75fnx
first round on mysql-shell based on the proxy code

this is mostly a verification if the proxy-code is flexible enough to handle 
all three scenarios of: client, server and forwarding (proxy)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
--[[
 
2
 
 
3
   Copyright (C) 2007 MySQL AB
 
4
 
 
5
   This program is free software; you can redistribute it and/or modify
 
6
   it under the terms of the GNU General Public License as published by
 
7
   the Free Software Foundation; version 2 of the License.
 
8
 
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
 
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program; if not, write to the Free Software
 
16
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
 
 
18
--]]
 
19
 
 
20
---
 
21
-- read_query() can rewrite packets
 
22
--
 
23
-- You can use read_query() to replace the packet sent by the client and rewrite
 
24
-- query as you like
 
25
--
 
26
-- @param packet the mysql-packet sent by the client
 
27
--
 
28
-- @return 
 
29
--   * nothing to pass on the packet as is, 
 
30
--   * proxy.PROXY_SEND_QUERY to send the queries from the proxy.queries queue
 
31
--   * proxy.PROXY_SEND_RESULT to send your own result-set
 
32
--
 
33
function read_query( packet )
 
34
        if string.byte(packet) == proxy.COM_QUERY then
 
35
                print("we got a normal query: " .. string.sub(packet, 2))
 
36
 
 
37
                proxy.queries:append(1, packet )
 
38
                -- generate a new COM_QUERY packet
 
39
                --   [ \3SELECT NOW() ]
 
40
                -- and inject it with the id = 2
 
41
                proxy.queries:append(2, string.char(proxy.COM_QUERY) .. "SELECT NOW()" )
 
42
 
 
43
                return proxy.PROXY_SEND_QUERY
 
44
        end
 
45
end
 
46
 
 
47
---
 
48
-- read_query_result() is called when we receive a query result 
 
49
-- from the server
 
50
--
 
51
-- we can analyze the response, drop the response and pass it on (default)
 
52
--
 
53
-- as we injected a SELECT NOW() above, we don't want to tell the client about it
 
54
-- and drop the result with proxy.PROXY_IGNORE_RESULT
 
55
-- 
 
56
-- @return 
 
57
--   * nothing or proxy.PROXY_SEND_RESULT to pass the result-set to the client
 
58
--   * proxy.PROXY_IGNORE_RESULT to drop the result-set
 
59
-- 
 
60
function read_query_result(inj)
 
61
        print("injected result-set: id = " .. inj.id)
 
62
 
 
63
        -- inj.id = 2 was assigned above in the  proxy.queries:append()
 
64
        -- 
 
65
        -- drop the resultset when we are done to hide it from the client
 
66
        if (inj.id == 2) then
 
67
                for row in inj.resultset.rows do
 
68
                        print("injected query returned: " .. row[0])
 
69
                end
 
70
 
 
71
                return proxy.PROXY_IGNORE_RESULT
 
72
        end
 
73
end